Spring Data Jpa - ManyToMany - delete entities of the join table - java

I have these two classes :
public class ClassA extends [...] implements [...] {
#ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinTable(name = JOIN_TABLE_NAME,
joinColumns = #JoinColumn(name = COLUMN_REF_A, referencedColumnName = COLUMN_ID_A),
inverseJoinColumns = #JoinColumn(name = COLUMN_REF_B, referencedColumnName = COLUMN_ID_B))
private List<ClassB> fieldClassB;
}
public class ClassB extends [...] implements [...] {
#ManyToMany(fetch = FetchType.LAZY, mappedBy = "fieldClassB", cascade = CascadeType.ALL)
private List<ClassA> fieldClassA;
}
When I delete ClassB (via the spring data jpa repository), Hibernate also deletes instances of ClassA, whereas I just want the rows in the JOIN_TABLE_NAME table to be deleted (the other issue is that, due to the cascade mode, deleting the ClassA entities also delete other ClassB referenced by these ClassA).
Is there any way to handle this without having to create the join entity and to replace the #ManyToMany annotations by #OneToMany referencing the new join entity ?

Cascade Remove in a manyToMany it's not only applied to the link table, but to the other side of the association as well.
So Cascade.ALL which inherit remove too is almost always a bad thing to have on a manyToMany as it ends up deleting things not only from association table.
What you want is to have add and remove method in your entities to do the work and keep both list synchronized:
public class ClassA extends [...] implements [...] {
#ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
#JoinTable(name = JOIN_TABLE_NAME,
joinColumns = #JoinColumn(name = COLUMN_REF_A, referencedColumnName = COLUMN_ID_A),
inverseJoinColumns = #JoinColumn(name = COLUMN_REF_B, referencedColumnName = COLUMN_ID_B))
private List<ClassB> fieldClassB;
public void addClassB(ClassB b) {
fieldClassB.add(b);
b.fieldClassA().add(this);
}
public void removeClassB(ClassB b) {
fieldClassB.remove(b);
b.fieldClassA().remove(this);
}
}

Related

JPA using one join table for two OneToMany entity

There's an Entity Class 'A' (supposed to be a Person),There's another Entity Class 'B' (supposed to be a Contract).
Entity 'A' has a relation #OneToMany to Class 'B' ( a person can sign alot of contracts). Entity 'B' also has a relation #OneToMany to Class 'A' (a contract can have many person signing it).
In this case, there's gonna be 2 JoinTable in database, but actually they both are somehow the same.
Is there anyway that i make them just using One JoinTable?
tnx for any help!
Looks like a #ManyToMany relation to me...
in class Person
#ManyToMany
#JoinTable(name="PERS_CONTRACTS")
public Set<Contract> getContracts() { return contracts; }
in class Contract
#ManyToMany(mappedBy="contracts")
public Set<Person> getSigners() { return signers; }
By using two #OneToMany there is no JoinTable.
you can use #ManyToMany like this
#ManyToMany
#JoinTable(
name="AB",
joinColumns=#JoinColumn(name="A_ID", referencedColumnName="ID"),
inverseJoinColumns=#JoinColumn(name="B_ID", referencedColumnName="ID"))
private List<B> bS;
Its a kind of Many to Many relationships. So it need just one junction table like person_contract in database. It will contains columns like:
Person_id
Contract_id
where both person_id & contract_id will be a composite unique key.
In hibernate it will be:
1. In Person table
#ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinTable(name = "person_contract ", joinColumns = {
#JoinColumn(name = "person_id", nullable = false, updatable = false) },
inverseJoinColumns = { #JoinColumn(name = "contract_id",
nullable = false, updatable = false) })
public Set<Contract> contracts;
In Contract table
#ManyToMany(fetch = FetchType.LAZY, mappedBy = "contracts")
public Set<Person> persons;
Situation:
You are complicating things here, the appropriate relationship between your Entities would be ManyToMany, because :
A person can sign many contracts.
And a contract can be signed by many persons.
And one JoinTable in this relationship is sufficient to give you all the requested details:
Who signed a given Contract.
Which Contracts have a Person signed.
Mapping:
So your mapping will be like this:
In your Person class:
#ManyToMany(mappedBy = "persons")
private Set<Contract> contracts= new HashSet<Contract>();
And in your Contract class:
#ManyToMany(cascade = CascadeType.ALL)
#JoinTable(
name = "PERSONS_CONTRACTS",
joinColumns = #JoinColumn(name = "CONTRACT_ID"),
inverseJoinColumns = #JoinColumn(name = "PERSON_ID")
)
private Set<Person> persons= new HashSet<Person>();
You can check this Hibernate Many-to-Many Association Annotations Example for further details.

empty DB joincolumn after cascade insert in bidirectional relationship

I have 2 classes that are connected by a bidirectional ManyToOne / OneToMany relationship:
Member in ClassA:
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "classA")
private List<ClassB> classBList = new ArrayList<ClassB>();
Member in ClassB:
#ManyToOne
#JoinColumn(name = "CLASSA_ID", referencedColumnName = "id")
private ClassA classA;
When I call classA.getClassBList().add(newClassB); a new DB entry for classB is created, but the DB column CLASSA_ID remains null.
of course all entities are defined in persistence.xml.
i appreciate any help, maybe it's just a little detail.
Thanks to bigGuy
My class looks like that now:
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
#JoinColumn(name = "CLASSA_ID", referencedColumnName = "id")
private List<ClassB> classBList = new ArrayList<ClassB>();
#ManyToOne
private ClassA classA
The annotation #JoinColumn indicates that this entity is the owner of the relationship.
In your case, owner is ClassB.
So, you should use this line to create relationship:
newClassB.setClassA(classA);
If you want to create relationships with line
classA.getClassBList().add(newClassB);
make ClassA owner of relationship (move #JoinColumn to classA).

join table on Cascaded model not updated

I have 2 entity classes (Class A and Class B) mapped to database tables. They are cascaded so that saving object of class A would reflect change in table concerned with Class B as well. Now I want to have a join table with primary key from Class B and primary key of new entity Class (Class C). I used standard hibernate #JoinTable annotation. But Join Table is not updated. Am I allowed to do so in hibernate?
Class A(Fieldvisit)
#OneToMany(mappedBy = "fieldVisitId")
#Cascade({CascadeType.SAVE_UPDATE,CascadeType.DELETE})
private Collection<Fieldvisitdetail> fieldvisitdetailCollection;
Class B(Fieldvisitdetail)
#JoinTable(name = "fieldvisitgeo",
joinColumns = {
#JoinColumn(name = "fieldVisitDetailId", referencedColumnName = "fieldVisitDetailId")},
inverseJoinColumns = {
#JoinColumn(name = "geoId", referencedColumnName = "geoId")})
#ManyToMany(mappedBy = "fieldvisitdetailCollection", cascade = CascadeType.ALL)
private Set<Geo> geoId=new HashSet<Geo>();
Class C (Geo)
#ManyToMany(cascade = CascadeType.ALL)
private Set<Fieldvisitdetail> fieldvisitdetailCollection= new HashSet<Fieldvisitdetail>();
Code for Saving to database
public void addFieldVisit(Fieldvisit visitReportInfo){
getSessionFactory().getCurrentSession().save(visitReportInfo);
}

Wrong update while cascading many-to-many relationship

I have a many-to-many relationship defined on hibernate like this:
User
public class User{
private List<UserCustomer> userCustomerList;
#OneToMany(fetch = FetchType.LAZY, mappedBy = "id.user", cascade=CascadeType.ALL)
public List<UserCustomer> getUserCustomerList() {
return userCustomerList;
}
}
UserCustomer
#Entity
#Table(name = "RTDB_USER_CUSTOMER")
#Component("userCustomerEntity")
#AssociationOverrides({
#AssociationOverride(name = "id.user",
joinColumns = #JoinColumn(name = "ID_USER")),
#AssociationOverride(name = "id.customer",
joinColumns = #JoinColumn(name = "ID_CUSTOMER")) })
public class UserCustomer {
#EmbeddedId
public UserCustomerId getId() {
return id;
}
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumns({ #JoinColumn(name = "ID_ROLE_CUSTOMER", referencedColumnName = "ID") }) public RoleCustomer getRoleCustomer() {
return roleCustomer;
}
}
So a user has a list of UserCustomer, that represent roles of users over customers. The problem is, that when we change a role over a customer and call update(), instead of one row updated we get all the rows updated with the same role. When we call merge() it starts to perform a lots of queries and then gives stackoverflow exception ¿Could this be a mapping problem?
Can you post the tables and updation code?
I think you are updating the role directly from UserCustomer which should be updating all the roles, as far as my understanding goes you do not want to update UserCustomer but only RoleCustomer. Try to fetch RoleCustomer and update it not the UserCustomer.

JPA many to many self referencing relation with additional column

I am working with JPA and use Hibernate as a provider to my SQL Server database.
I need a many-to-many self referencing relation that has an additional column or even more additional columns.
That is my current code. I am getting exceptions by Hibernate:
#Entity
public class Person {
#OneToMany(cascade = CascadeType.ALL, mappedBy = "person", fetch = FetchType.EAGER)
private Set<Relation> relations;
#OneToMany(cascade = CascadeType.ALL, mappedBy = "relPerson", fetch = FetchType.EAGER)
private Set<Relation> inverseRelations;
}
#Entity
public class Relation implements Serializable {
#Id
#ManyToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.EAGER)
#PrimaryKeyJoinColumn(name = "PersonID", referencedColumnName = "id")
private Person person;
#Id
#ManyToOne(cascade = CascadeType.ALL, optional = false, fetch = FetchType.EAGER)
#PrimaryKeyJoinColumn(name = "RelPersonId", referencedColumnName = "id")
private Person relPerson;
}
During runtime i get an exception from hibernate:
org.hibernate.TransientObjectException: object references an unsaved transient instance
Is there any way to implement this a little bit more intelligent and nicely?? Without getting that exception.
Thanks,
ihrigb
If an object not associated with a Hibernate Session, the object will be Transient.
An instance of Relation list may be Transient(Normally, There is no identifier value for that instance) when you save Person.
Here is better way to understand object state.

Categories

Resources