JPA: a foreign key, which is also a primary key mapping

I tried to solve it all day, but no luck! I also tried to read most of the manuals online, but as you all know, they are all full of useless examples that do not reflect what you need in the real world.

So here is my situation:

Database:

table: vehicles (vehicleId, brand, model, devYear, regNumber) <- vehicleId is PrimaryKey

table: additional (vehicleId, allowSmoke, allowFood, allowDrinks, airConditioner) <- vehicleId is a PC and FK.

The fact is that if I have a Car class and a TravelExtras class that are mapped to a database, I want the Car class to have the TravelExtras travelExtras attribute, as well as the methods for getting and setting it.

Unfortunately, no matter what I tried, when I try to save the object in a data file, I get various errors.

Here is an illustration:

EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "NaStopPU" ); EntityManager entitymanager = emfactory.createEntityManager( ); entitymanager.getTransaction( ).begin( ); TravelExtra travelExtra = new TravelExtra(); entitymanager.persist(travelExtra); Vehicle vehicle = new Vehicle(2L, "10152487958556242", "Mazda", "626", "334343", 2005, 4); vehicle.setTravelExtra(travelExtra); entitymanager.persist(vehicle); entitymanager.getTransaction().commit(); entitymanager.close( ); emfactory.close( ); 

Does anyone know which annotations to use for this one-on-one case?

+7
java database orm jpa
source share
3 answers

The Java Persistence wikibook has a section called Primary Keys through OneToOne and ManyToOne Relationships , which apparently indicate what you want.

If I read it correctly, for your case it will look something like this:

 class Vehicle { @Id @Column(name = "EXTRAS_ID") private long extrasId; @OneToOne(mappedBy="vehicle", cascade=CascadeType.ALL) private TravelExtra extras; } class TravelExtras { @Id @Column(name = "VEHICLE_ID") private long vehicleId; @OneToOne @PrimaryKeyJoinColumn(name="VEHICLE_ID", referencedColumnName="EXTRAS_ID") private Vehicle vehicle; public TravelExtras(Vehicle vehicle) { this.vehicleId = vehicle.getId(); this.vehicle = vehicle; } } 

Note that one of your entities will need to make sure that it has the same identifier as the other, which is executed in the example by the TravelExtras constructor, requiring the Vehicle to be attached to it.

+11
source share

You can map your classes, for example, with Netbeans. It will generate annotations. The problem may be your dao layer. You must transfer the objects correctly. For example, you cannot save travelExtra without Vehicle. Also keep in mind ownership of the party.

+1
source share

Why aren't you using the @Embedded object? When using the built-in object, you get the logical separation that you want in your code and maintain the compatibility of your database with the rules of entity-relational normalization.

It is strange to think about a one-to-one relationship, because although JPA / Hibernate allows this, all data must be stored in one table, which simplifies modeling, as well as simplifies queries and improves database performance, eliminating the need for Join operations.

When using built-in objects, you donโ€™t have to worry about matching identifiers and fancy relationships, as your ORM is able to understand that you are simply doing code separation, rather than requiring an actual one-to-one relationship between tables.

 class Vehicle { @Id @Column(name = "ID") private long vehicleId; @Column(name = "BRAND") private String brand; @Column(name = "MODEL") private String model; @Column(name = "DEV_YEAR") private int devYear; @Column(name = "REG_NUMBER") private int regNumber; @Embedded private TravelExtra extras; // Constructor, getters and setters... } 

.

 @Embeddable class TravelExtras { @Column(name = "ALLOW_SMOKE") private boolean allowSmoke; @Column(name = "ALLOW_FOOD") private boolean allowFood; @Column(name = "ALLOW_DRINKS") private boolean allowDrinks; @Column(name = "AIR_CONDITIONER") private boolean airConditioner; // Default Constructor, getters and setters... } 
+1
source share

All Articles