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.
T_E_AIRtable represent? - Tom Anderson@MapsId, and (3) you probably wantequalsandhashCodeimplementations onUserConfig, although i'm not sure about that. - Tom Andersonequals&hashcodein each class...and i'm trying to work on business-id, not on table-id. Thank you Tom ! - MychaL