One To Many field does not update in JPA - java

#Entity
public class Conference {
#ManyToOne
#JoinColumn(name = "host_id")
#JsonManagedReference
private User host;
#ManyToMany(fetch = FetchType.EAGER)
#JoinTable(name = "PARTICIPANT_CONFERENCE",
joinColumns = #JoinColumn(name = "CONFERENCE_ID_FRK"),
inverseJoinColumns = #JoinColumn(name = "PARTICIPANT_ID_FRK")
)
#JsonManagedReference
private Set<User> participants;
}
#Entity
public class User {
#ManyToMany(
targetEntity = Conference.class,
fetch = FetchType.EAGER,
cascade = CascadeType.REMOVE,
mappedBy = "participants"
)
#JsonBackReference
private Set<Conference> conferenceSet;
#OneToMany(
targetEntity = Conference.class,
mappedBy = "host",
cascade = CascadeType.REMOVE,
fetch = FetchType.EAGER
)
#JsonBackReference
private Set<Conference> conferenceHostSet;
}
I used JPA and have a problem that OneTOMany field does not update in JPA.
If I added host in Conference, but User's host set did not update.
So, I tried to update User's host set, too.
Set<Conference> conferences = form.getHost().getConferenceHostSet();
conferences.add(conference);
userRepository.save(form.getHost().setConferenceSet(conferences));
It works well, but User's participant set also updated.
How can I update only host?

You have set cascading to only work for "remove" you need to set it up for all of the actions you want.

Related

Many To Many collection always null (Spring Boot)

I have a problem with an entity that has a many to many relationship.
If I try to access the collection referring to many to many (via getter) I always have a null pointer exception. I also added fethType.EAGER and initialized the collection, but I get the same error.
Does anyone know how to fix it?
#Entity
public class Film implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column
private int id;
//other
#ManyToMany(fetch = FetchType.EAGER, mappedBy = "film")
private Set<Cinema> cinema = new HashSet<>();
//getter and setter
#Entity
public class Cinema implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column
private int id;
#ManyToMany(fetch = FetchType.EAGER,
cascade = {CascadeType.PERSIST, CascadeType.MERGE})
#JoinTable(name = "cinema_film", joinColumns = {#JoinColumn(name = "cinema_id")},
inverseJoinColumns = {#JoinColumn(name = "film_id")})
private Set<Film> film = new HashSet<>();
I have the exception when I try
cdb.getFilm().add(fdb);
Try doing fetch = FetchType.LAZY and cascade = CascadeType.MERGE in your Cinema class like this.
#ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.MERGE)
and fetch = FetchType.LAZY, cascade = CascadeType.ALL in your Film class.

How to avoid removing entity from many to many in jpa

I have this mapping at Person entity:
#ManyToMany(fetch = FetchType.LAZY)
#JoinTable(name = "PersonAddress", joinColumns = { #JoinColumn(name = "personId") }, inverseJoinColumns = {
#JoinColumn(name = "addressId") })
private Set<Address> addresses;
And this at Address entity:
#ManyToMany(mappedBy = "addresses")
private Set<Person> owners;
I tried all cascade options available, but everytime when I save a Person entity it removes all relations with addresses. Is it possible to keep addresses when saving Person?
its not recommended to use CascadeType.REMOVE or CascadeType.ALL in to-many associations, you should implement the removal of child entities yourself.
Here the link it explains with details how to do the implementation.
https://thorben-janssen.com/avoid-cascadetype-delete-many-assocations/

Hibernate #JoinTable not working properly

I'm having this weird problem going on with my application when it comes to #JoinTable use.
I have 3 entities: EntityA, EntityB and EntityC.
EntityA.class
#OneToMany(fetch = FetchType.LAZY, mappedBy = "entityA")
public Set<EntityB> getEntityBSet() {
return entityBSet;
}
EntityB.class
#ManyToOne
#JoinColumn(name = "ID_ENTITY_A", nullable = false)
public EntityA getEntityA() {
return entityA;
}
#ManyToOne
#JoinTable(name = "B_AND_C",
joinColumns = { #JoinColumn(name = "ID_ENTITY_B") },
inverseJoinColumns = {#JoinColumn(name = "ID_ENTITY_C") })
public EntityC getEntityC() {
return entityC;
}
EntityC.class
#OneToMany(cascade = CascadeType.ALL, mappedBy="entityC")
public Set<EntityB> getEntityBSet() {
return entityBSet;
}
Everything works fine until I try to access entityB through entityA.getEntityBSet().
Hibernate doesnt generate the proper SQL for the relationship entityB.entityC, creating a Cartesian product:
SELECT ...
FROM ENTITY_B, B_C, ENTITY_C
WHERE B_C.ID_ENTITY_C = ENTITY_C.ID_ENTITY_C (+)
AND ENTITY_B.ID_ENTITY_A = ?
There should be the join between ENTITY_B and B_C:
AND ENTITY_B.ID_ENTITY_B = B_C.ID_ENTITY_B
On the other hand, when loading entityB through session.get(), the generated SQL is correct for that relationship (entityB.entityC).
Any help appreciated.

Many to Many Not Respecting Update and Insert

I am trying to setup and Many to Many relationship between users and roles. I am mostly using JPA Repositories but I also tried using and EntityManger.
I have the following in my main User object.
#ManyToMany(fetch = FetchType.LAZY)
#JoinTable(name = "svcAuthUserRolev2", schema="dbo", joinColumns = {
#JoinColumn(name = "user_id", updatable=false,insertable=false, nullable = false) },
inverseJoinColumns = { #JoinColumn(name = "role_id",
updatable=false,insertable=false, nullable = false) })
private Set<AuthRoleEntity> roles;
And the following in my Roles object
#ManyToMany(fetch = FetchType.LAZY, mappedBy = "roles")
private Set<AuthUserEntity> users;
No matter what I do if I make changes to the roles on a user when saving they are persisted and this is not what I would like. I want roles on the user object to be read only.
I'm not sure why updateable and insertable are not working, I haven't used those attributes much. One possible solution is to make AuthRoleEntity the owning entity of the many-to-many relationship. Just move the #JoinTable annotation to the AuthRoleEntity and put the mappedBy on the AuthUserEntity.

Hibernate 4 remove entity with oneToMany collection

I'm trying to delete User entity from DB
//whatever
#OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "user", orphanRemoval = true)
private Set<Filter> filters = new HashSet<Filter>();
//whatever
//whatever
#ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH, CascadeType.DETACH})
#JoinColumn(name = "user_id", nullable = false)
private User user;
//whatever
but getting:
ERROR SqlExceptionHelper:146 - Cannot delete or update a parent row: a foreign key constraint fails (web_app_db.filters, CONSTRAINT FK_8oeay5yddrcgd4i71m1yda1hb FOREIGN KEY (user_id) REFERENCES users (user_id))
orphanRemoval = true not working, what should I do?
Try this:
#ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.ALL})
#JoinColumn(name = "user_id", nullable = false)
private User user;

Categories

Resources