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?
java database orm jpa
Joro seksa
source share