Reputation: 1140
I'm new to Hibernate and JPA in Spring, so this is probably a beginner error. I'm having trouble persisting an object with its association.
For example:
Dog dog = new Dog();
Breed breed = dao.getBreedById(1); (gets persistent object)
dog.setName("Pluto");
dog.setBreed(breed);
save(dog);
The dog and all fields save, but the breed association does not save in the database. I don't get any errors. It just doesn't save. Anyone know what I'm doing wrong?
I traced the issue to an annotation that had been in place ... that left me hitting myself afterwards. -.-
I had:
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "breed_id", insertable = false, updatable = false)
private Breed breed;
Sigh. Removing the insertable and updatable clause fixed the problem.
Upvotes: 1
Views: 297
Reputation: 5087
You save the dog into the DB. If what you want is breed have the dog, you should set this manually as well. There is no auto-relation in JPA
Dog dog = new Dog();
Breed breed = dao.getBreedById(1); (gets persistent object)
dog.setName("Pluto");
dog.setBreed(breed);
breed.getDogCollection.add(dog);//you have to maintain relationship manually
save(dog);
Upvotes: 1