I have two table one is ObjectP and Another AccessObject
ObjectP AccessObject
ObjectId accessObjectId
Name groupId
Description objectId
I have applied a one-to-one mapping(which is working fine) in AccessObject as follows:
#OneToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "objectId" , insertable = false, updatable = false, nullable = false, unique = true)
private ObjectP objectP;
When I access ObjectP I want to navigate related AccessObject too. How can I do that?
In AccessObject you'll have the side owning the association:
#OneToOne
#JoinColumn(name = "objectId" , nullable = false, unique = true)
private ObjectP objectP;
Then in ObjectP you have the inverse side:
#OneToOne(mappedBy="objectP")
private AccessObject accessObject
fetch = FetchType.EAGER is the default *-To-One fetch strategy so it's redundant
the owning side must not have insertable/updatable=false, since you want this side to own the association
the mappedBy is how you mark the inverse side in bi-directional associations
Related
I always use the following method to implement ManyToOne in class :
#Column(name = "buyer_id")
private Long buyerId;
#ManyToOne
#JoinColumn(name = "buyer_id", insertable = false, updatable = false)
UserGroup buyer;
However, I have a question whether it is better to use the following code:
#ManyToOne
#JoinColumn(name = "buyer_id", insertable = false, updatable = false)
UserGroup buyer;
or not?
In the first case, I always set the id value obtained in buyerId for saving after saving the UserGroup, but in the second case, I put the userGroup model completely (after save) in my entity then save entity.
I'm not sure if the method I use is the best.
I searched the internet but could not find a technical reason that the latter is not appropriate.
I am trying to implement a delete function but all I get is this ORA-02292 ERROR:
Caused by: java.sql.SQLIntegrityConstraintViolationException: ORA-02292: Integritäts-Constraint (VDMA.FK892DE8B473F40868) verletzt - untergeordneter Datensatz gefunden
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:447)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:396)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:951)
I have an Entity (MainEntity) that has an n:m relationship with its child entity.
For example: A User can have multiple cars, each car can be driven by different users.
When a User is deleted, I want the associations between the User and the Car to be deleted as well. That´s why I thought I could do the following :
User Entity
#ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinTable(name = "user_car", joinColumns = {#JoinColumn(name = DATABASE_COLUMN_ID, nullable = false, updatable = false) }, inverseJoinColumns = {#JoinColumn(name = DATABASE_COLUMN_TYPE_ID,
nullable = true, updatable = false) })
private Set<UserCar> userCars;
Car Entity
#ManyToMany(fetch = FetchType.LAZY, mappedBy = "userCars")
private Set<User> users;
The result is :
The tables get created, the data gets persisted - everything works fine.
EXCEPT deleting entries: As soon as I try to delete a User and the User has a car (and therefore a user : car relationship) I do get the errors shown above.
If the user doesn´t have any cars I can delete him without any issues. So the problem must be the constraint in the USER_CAR table.
Clear userCars as well:
user.getUserCars().clear()
This will break the association between user and associated cars (it will delete the corresponding records from the junction table).
Also, you don't want CascadeType.ALL on many-to-many associations, because it implicitly contains CascadeType.REMOVE. Using it means that removal will be cascaded to cars as well, although there are other users who are associated with those cars.
Pls make sure you delete the child records, adding the orphanRemoval will help
#ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL,orphanRemoval=true)
#JoinTable(name = "user_car", joinColumns = {#JoinColumn(name = DATABASE_COLUMN_ID, nullable = false, updatable = false) }, inverseJoinColumns = {#JoinColumn(name = DATABASE_COLUMN_TYPE_ID,
nullable = true, updatable = false) })
private Set userCars;
This will delete all cars assoicated with a user.
For example I have two tables Employee and Department and I am using
#ManyToOne
#JoinColumn(name = "DEPT_ID", nullable = true, insertable = false, updatable = false)
private Department department;
in Employee.
Here I want to add one constraint that, whenever one of the column of Department is updated to some value then it should not get updated if it has child records in Employee.
Can I achieve this by using CascadeType?
Or is there any other way by which I can define custom constraint?
If your intention is to prohibit memory to database sync when Department is updated, then you're done as you have insertable=false, update=false:
#JoinColumn(name = "DEPT_ID", nullable = true, insertable = false, updatable = false)
i would like to create an application in this context : Zk 6, Spring v3.1.1, JPA 2.0, Hibernate 4.1.4, all with annotations but i have some pb with JPA concept.
Here are a type of case study :
3 tables, all linked via a join table ; we are dealing with cardinality 0, n.
So we have T_E_USER, T_E_TYPE and T_E_AIR.
Each table has a numeric ID, and a simple VARCHAR field.
A join table is created with T_J_USR_TPE_AIR with the 3 ID referenced by foreign keys forming a composed primary key.
I'm using Hibernate Tools for generate my entities (version JPA).
And that's where the problems start ....
I have, in each entity class, an attribute of type set with annotation # OneToMany.
I have a class representing the join that has an id attribute of complex type (another class) with an annotation EmbeddedId for a composite key.
And attributes representing the three entities with annotations # ManyToOne.
Here are my questions, because that's where I'm confused:
which should i set into the "mappedBy" attribute in the annotation # OneToMany of my entities?
Am I forced to do a class entity representing the join?
How does the CASCADE? Is it possible to use it in this context to enrich the join table "automatically"? Or should I manually instantiate the class representative of the join in order to persist the information myself?
A big thank you in advance for any kind soul who could give me a helping hand.
Thank you for your answers but one said "yes" when the other says "no" lol
Here's what I did during the day but I have not yet been tested.
In each entity table, i added a #OneToMany relation with mappedBy setted to the attribute defined in "join" entity :
#OneToMany(fetch = FetchType.LAZY,
mappedBy = "aircraft",
cascade = { CascadeType.REMOVE })
private Set<UserConfig> userConfigs = new HashSet<UserConfig>(0);
...
#OneToMany(fetch = FetchType.LAZY,
mappedBy = "userAccount",
cascade = { CascadeType.REMOVE })
private Set<UserConfig> userConfigs = new HashSet<UserConfig>(0);
...
#OneToMany(fetch = FetchType.LAZY,
mappedBy = "referenceType",
cascade = { CascadeType.REMOVE })
private Set<UserConfig> userConfigs = new HashSet<UserConfig>(0);
And i created a new Entity for the join table.
#Entity
#Table(name = "T_J_USR_RFT_AIR_URA")
public class UserConfig implements java.io.Serializable {
#EmbeddedId
#AttributeOverrides({
#AttributeOverride(name = "airId",
column = #Column(name = "URA_AIR_ID", nullable = false)),
#AttributeOverride(name = "usrId",
column = #Column(name = "URA_USR_ID", nullable = false)),
#AttributeOverride(name = "rftId",
column = #Column(name = "URA_RFT_ID", nullable = false))
})
private UserConfigId id;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "URA_RFT_ID", nullable = false, insertable = false, updatable = false)
private ReferenceType referenceType;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "URA_USR_ID", nullable = false, insertable = false, updatable = false)
private UserAccount userAccount;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "URA_AIR_ID", nullable = false, insertable = false, updatable = false)
private Aircraft aircraft;
...
getter & setter
}
Where UserConfigId is :
#Embeddable
public class UserConfigId implements java.io.Serializable {
#Column(name = "URA_AIR_ID", nullable = false)
private Integer airId;
#Column(name = "URA_USR_ID", nullable = false)
private Integer usrId;
#Column(name = "URA_RFT_ID", nullable = false)
private Integer rftId;
...
getter & setter
}
What do you think about this practice ?
I just used "cascade" if an object of the join table is deleted in order to delete all element associated in the join.
Is it all right ?
Anyway thank you Tom, i will analyzed your link.
Thank you JMelnyk too.
You are welcome if you want to demonstrate what are the best practices for this case.
Three-way joins are tricky. I think what you've done, using an entity for the join table, is probably the right thing to do. To answer your questions:
Your #OneToMany attributes refer to the entity mapping the join table; they should be mappedBy the appropriate #ManyToOne attribute in that entity.
Yes, unfortunately, an entity for the join table is the best way to do this.
Cascades can be used to automatically add objects to the database, but not to create objects. You will need to create instances of the join entity in code.
which should i set into the "mappedBy" attribute in the annotation #
OneToMany of my entities?
mappedBy attribute represents a property name you are joining on. Read more...
e.g. AnyEntity holds List<Employee> which is joined on (mappedBy) department property in Employee entity, and that department property holds the association.
Am I forced to do a class entity representing the join?
No, you do not provide an entity class for join tables.
How does the CASCADE? Is it possible to use it in this context to
enrich the join table "automatically"? Or should I manually
instantiate the class representative of the join in order to persist
the information myself?
Yes it is possible to enrich associations of the entity and itself by marking associations with desired cascade type.
e.g. We have a Department which holds List<Employee> and I put CascadeType.PERSIST on employees. Now we populate department objects with its properties and employees. When we are finished, we persist only the department, and it will cascade operation to employees.
#ManyToOne(fetch = LAZY)
#JoinColumn(name = COL_GROUP_ID, nullable = false, insertable = false, updatable = false, referencedColumnName = COL_ID)
#OnDelete(action = CASCADE)
#Cascade(value = DELETE_ORPHAN)
private Group group;
How to enforce orphan deletion on a ManyToOne relationship, the above code snippet worked for us in Hibernate 3.3.x, but post migration to 3.6.5.Final it shows up as a WARNING in the code. is there a equivalent flag like orphanRemoval = true which is applied on a #OneToMany notation?
You can not apply ORPHAN_REMOVAL to MANY_TO_ONE side.
Suppose you have an entity City which has #OneToMany Citizen and on the other side you have #ManyToOne City in Citizen entity. In your scenario removing one citizen will lead to removing the whole city, thus ORPHAN_REMOVAL is only applicable to XXX_TO_Many side