Hi All,
I created 3 tables
1-Tprivs
2-Trole
3-Trole_Tprivs_Rel contains composite key of Trole and Tprivs
The entities that were generated by jboss tools are as follow
Tprivs class
Code:
@Entity
@Table(name = "TPrivs", schema = "dbo", catalog = "testdata")
public class Tprivs implements java.io.Serializable {
private long id;
private String name;
private Set<TroleTprivsRel> troleTprivsRels = new HashSet<TroleTprivsRel>(0);
public Tprivs() {
}
public Tprivs(long id) {
this.id = id;
}
public Tprivs(long id, String name, Set<TroleTprivsRel> troleTprivsRels) {
this.id = id;
this.name = name;
this.troleTprivsRels = troleTprivsRels;
}
@Id
@Column(name = "Id", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.AUTO)
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "Name", length = 50)
@Length(max = 50)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "tprivs")
public Set<TroleTprivsRel> getTroleTprivsRels() {
return this.troleTprivsRels;
}
public void setTroleTprivsRels(Set<TroleTprivsRel> troleTprivsRels) {
this.troleTprivsRels = troleTprivsRels;
}
}
Trole class
Code:
@Entity
@Table(name = "TRole", schema = "dbo", catalog = "testdata")
public class Trole implements java.io.Serializable {
private long id;
private String name;
private Set<TroleTprivsRel> troleTprivsRels = new HashSet<TroleTprivsRel>(0);
public Trole() {
}
public Trole(long id) {
this.id = id;
}
public Trole(long id, String name, Set<TroleTprivsRel> troleTprivsRels) {
this.id = id;
this.name = name;
this.troleTprivsRels = troleTprivsRels;
}
@Id
@Column(name = "Id", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.AUTO)
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "Name", length = 50)
@Length(max = 50)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "trole")
public Set<TroleTprivsRel> getTroleTprivsRels() {
return this.troleTprivsRels;
}
public void setTroleTprivsRels(Set<TroleTprivsRel> troleTprivsRels) {
this.troleTprivsRels = troleTprivsRels;
}
}
TRole_Tprivs_Rel class
Code:
@Entity
@Table(name = "TRole_TPrivs_Rel", schema = "dbo", catalog = "testdata")
public class TroleTprivsRel implements java.io.Serializable {
private TroleTprivsRelId id;
private Trole trole;
private Tprivs tprivs;
private Boolean c;
private Boolean r;
private Boolean u;
private Boolean d;
public TroleTprivsRel() {
}
public TroleTprivsRel(TroleTprivsRelId id, Trole trole, Tprivs tprivs) {
this.id = id;
this.trole = trole;
this.tprivs = tprivs;
}
public TroleTprivsRel(TroleTprivsRelId id, Trole trole, Tprivs tprivs,
Boolean c, Boolean r, Boolean u, Boolean d) {
this.id = id;
this.trole = trole;
this.tprivs = tprivs;
this.c = c;
this.r = r;
this.u = u;
this.d = d;
}
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "troleId", column = @Column(name = "TRole_Id", nullable = false)),
@AttributeOverride(name = "tprivsId", column = @Column(name = "TPrivs_Id", nullable = false)) })
@NotNull
public TroleTprivsRelId getId() {
return this.id;
}
public void setId(TroleTprivsRelId id) {
this.id = id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "TRole_Id", nullable = false, insertable = false, updatable = false)
@NotNull
public Trole getTrole() {
return this.trole;
}
public void setTrole(Trole trole) {
this.trole = trole;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "TPrivs_Id", nullable = false, insertable = false, updatable = false)
@NotNull
public Tprivs getTprivs() {
return this.tprivs;
}
public void setTprivs(Tprivs tprivs) {
this.tprivs = tprivs;
}
@Column(name = "C")
public Boolean getC() {
return this.c;
}
public void setC(Boolean c) {
this.c = c;
}
@Column(name = "R")
public Boolean getR() {
return this.r;
}
public void setR(Boolean r) {
this.r = r;
}
@Column(name = "U")
public Boolean getU() {
return this.u;
}
public void setU(Boolean u) {
this.u = u;
}
@Column(name = "D")
public Boolean getD() {
return this.d;
}
public void setD(Boolean d) {
this.d = d;
}
}
TroleTprivsRelId class
Code:
@Embeddable
public class TroleTprivsRelId implements java.io.Serializable {
private long troleId;
private long tprivsId;
public TroleTprivsRelId() {
}
public TroleTprivsRelId(long troleId, long tprivsId) {
this.troleId = troleId;
this.tprivsId = tprivsId;
}
@Column(name = "TRole_Id", nullable = false)
public long getTroleId() {
return this.troleId;
}
public void setTroleId(long troleId) {
this.troleId = troleId;
}
@Column(name = "TPrivs_Id", nullable = false)
public long getTprivsId() {
return this.tprivsId;
}
public void setTprivsId(long tprivsId) {
this.tprivsId = tprivsId;
}
public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof TroleTprivsRelId))
return false;
TroleTprivsRelId castOther = (TroleTprivsRelId) other;
return (this.getTroleId() == castOther.getTroleId())
&& (this.getTprivsId() == castOther.getTprivsId());
}
public int hashCode() {
int result = 17;
result = 37 * result + (int) this.getTroleId();
result = 37 * result + (int) this.getTprivsId();
return result;
}
}
Now I want to add relation between a Trole object and Tprivs Object which I do as follow:
Code:
Tprivs p;
session.beginTransaction();
long id=1;
p=(Tprivs)session.load(Tprivs.class,id);
Trole r=(Trole)session.load(Trole.class,id);
TroleTprivsRelId rpid=new TroleTprivsRelId(r.getId(), p.getId());
TroleTprivsRel tprel=new TroleTprivsRel();
tprel.setId(rpid);
r.getTroleTprivsRels().clear();
p.getTroleTprivsRels().clear();
r.getTroleTprivsRels().add(tprel);
p.getTroleTprivsRels().add(tprel);
session.save(p);
session.save(r);
session.getTransaction().commit();
session.flush();
But when I look in the TRole_TPrivs_Rel table I do not find the relation between the two entities
Any help
Thanks,