hello ive got a mapping problem by using secondarytable annotations:
i've got 2 tables:
SEMTYP and SEMPREIS
many SEMTYP to one SEMPREIS
Now I want to map these two beutiful tables into one objects! yeah ;-)
My first Association was following:
Code:
@Entity
@Table(name = "SEMTYP", schema = "X12345", uniqueConstraints = {})
@SecondaryTable(name="SEMPREIS", pkJoinColumns=@PrimaryKeyJoinColumn(name="DAUER", referencedColumnName="DAUER"))
public class Semtyp implements java.io.Serializable {
// Fields
private Double dauer;
private Integer preis;
private Date giltab;
private String semcode;
private String titel;
private Integer maxteiln;
private Set<Seminar> seminars = new HashSet<Seminar>(0);
// Constructors
/** default constructor */
public Semtyp() {
}
/** minimal constructor */
public Semtyp(Double dauer, Integer preis) {
this.dauer = dauer;
this.preis = preis;
}
public Semtyp(Double dauer, Integer preis, Date giltab, String semcode,
String titel, Integer maxteiln) {
super();
this.dauer = dauer;
this.preis = preis;
this.giltab = giltab;
this.semcode = semcode;
this.titel = titel;
this.maxteiln = maxteiln;
}
// Property accessors
@Id
@Column(name = "DAUER", unique = true, nullable = false, insertable = true, updatable = true)
public Double getDauer() {
return this.dauer;
}
public void setDauer(Double dauer) {
this.dauer = dauer;
}
@Column(name = "PREIS", table="SEMPREIS", unique = false, nullable = false, insertable = true, updatable = true)
public Integer getPreis() {
return this.preis;
}
public void setPreis(Integer preis) {
this.preis = preis;
}
@Temporal(TemporalType.DATE)
@Column(name = "GILTAB", table="SEMPREIS", unique = false, nullable = true, insertable = true, updatable = true, length = 10)
public Date getGiltab() {
return this.giltab;
}
public void setGiltab(Date giltab) {
this.giltab = giltab;
}
/**
* @return the semcode
*/
@Column(name="SEMCODE")
public String getSemcode() {
return semcode;
}
/**
* @param semcode the semcode to set
*/
public void setSemcode(String semcode) {
this.semcode = semcode;
}
/**
* @return the titel
*/
@Column(name="TITEL")
public String getTitel() {
return titel;
}
/**
* @param titel the titel to set
*/
public void setTitel(String titel) {
this.titel = titel;
}
/**
* @return the maxteiln
*/
@Column(name="MAXTEILN")
public Integer getMaxteiln() {
return maxteiln;
}
/**
* @param maxteiln the maxteiln to set
*/
public void setMaxteiln(Integer maxteiln) {
this.maxteiln = maxteiln;
}
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "semtyp")
public Set<Seminar> getSeminars() {
return this.seminars;
}
public void setSeminars(Set<Seminar> seminars) {
this.seminars = seminars;
}
}
By inserting objects I got an DB2-SQL Error (530), which says that I'm injuring the database integrity. The reason is that im trying to fill the foreign key at the SEMTYP table, before filling it as a primary key at the SEMPREIS table.
If I'm doing the secondary table inverse(joining the SEMTYP table into the Sempreis table) its going fine!
BUT WHY THIS SOLUTION DOESNT WORK?!?!?!
The Semtyp table is also a "1 to many" relationship to another table, thats the reason why i need it as mastertable?
CODE of the 1 to many from SEMTYP
Code:
@Entity
@Table(name = "SEMINAR", schema = "X12345", uniqueConstraints = {})
public class Seminar implements java.io.Serializable {
// Fields
private Integer semnr;
private Semtyp semtyp;
private Refrent refrent;
private Date termin;
private String kursort;
private Set<Semres> semreses = new HashSet<Semres>(0);
// Constructors
/** default constructor */
public Seminar() {
}
/** minimal constructor */
public Seminar(Integer semnr) {
this.semnr = semnr;
}
/** full constructor */
public Seminar(Integer semnr, Semtyp semtyp, Refrent refrent, Date termin,
String kursort) {
this.semnr = semnr;
this.semtyp = semtyp;
this.refrent = refrent;
this.termin = termin;
this.kursort = kursort;
}
// Property accessors
@Id
@Column(name = "SEMNR", unique = true, nullable = false, insertable = true, updatable = true)
public Integer getSemnr() {
return this.semnr;
}
public void setSemnr(Integer semnr) {
this.semnr = semnr;
}
@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
@JoinColumn(name = "SEMCODE", unique = false, nullable = true, insertable = true, updatable = true)
public Semtyp getSemtyp() {
return this.semtyp;
}
public void setSemtyp(Semtyp semtyp) {
this.semtyp = semtyp;
}
@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
@JoinColumn(name = "REFNR", unique = false, nullable = true, insertable = true, updatable = true)
public Refrent getRefrent() {
return this.refrent;
}
public void setRefrent(Refrent refrent) {
this.refrent = refrent;
}
@Temporal(TemporalType.DATE)
@Column(name = "TERMIN", unique = false, nullable = true, insertable = true, updatable = true, length = 10)
public Date getTermin() {
return this.termin;
}
public void setTermin(Date termin) {
this.termin = termin;
}
@Column(name = "KURSORT", unique = false, nullable = true, insertable = true, updatable = true, length = 32)
public String getKursort() {
return this.kursort;
}
public void setKursort(String kursort) {
this.kursort = kursort;
}
@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "seminar")
public Set<Semres> getSemreses() {
return this.semreses;
}
public void setSemreses(Set<Semres> semreses) {
this.semreses = semreses;
}
}
I want a running programming ;)