Hi,
I have two tables with one to many relashionship, both with composite keys.
I manage to write a test case where I can insert a new value, bit I can't load back the inserted value, I always become a null object. Please tell me, what am I doing wrong.
Code:
@Test
public void testSaveCashEdiMain() throws Exception {
CashEdiMain cashEdiMain=new CashEdiMain();
CashEdiMainPK cashEdiMainPK=new CashEdiMainPK("1","bb");
cashEdiMain.setCashEdiMainPK(cashEdiMainPK);
cashEdiMain.setAbsender("absender");
cashEdiMain.setEmpfaenger("empfänger");
cashEdiMain.setErstelldatum(Calendar.getInstance().getTime());
ArrayList<CashEdiPosition> listPos=new ArrayList<CashEdiPosition>();
CashEdiPosition position=new CashEdiPosition();
CashEdiPositionPK cashEdiPositionPK=new CashEdiPositionPK("1",0,"bb");
position.setCashEdiPositionPK(cashEdiPositionPK);
listPos.add(position);
cashEdiMain.setCashEdiPositionList(listPos);
cashEdiMainDAO.saveOrUpdateCashEdiMain(cashEdiMain);
CashEdiMain cashEdiMain1=cashEdiMainDAO.findCashEdiMain(cashEdiMainPK);
System.out.println("cashEdiMain1:"+cashEdiMain1);
}
Code:
public CashEdiMain findCashEdiMain(CashEdiMainPK cashEdiMainPK) throws Exception {
Transaction tx = null;
try {
initSession();
tx=session.getTransaction();
if(!tx.isActive())
tx=session.beginTransaction();
/*Query createQuery = session.createQuery("from CashEdiMain u where u.cashEdiMainPK.dateiId=:dateiID and u.cashEdiMainPK.lieferNve=:lieferNVE");
createQuery.setParameter("dateiID", cashEdiMainPK.getDateiId());
createQuery.setParameter("lieferNVE", cashEdiMainPK.getLieferNve());
Query createQuery = session.createQuery("from CashEdiMain u where u.cashEdiMainPK=:pk ");
createQuery.setParameter("pk", cashEdiMainPK);
System.out.println(createQuery.list().size());
CashEdiMain ergebnis = (CashEdiMain)createQuery.list().get(0);*/
CashEdiMain ergebnis= (CashEdiMain)session.get(CashEdiMain.class,cashEdiMainPK);
tx.commit();
session.close();
return ergebnis;
} catch (HibernateException e) {
LOG.error("find all CashEdiMain fehlgeschlagen.", e);
tx.rollback();
throw e;
}
}
Code:
public class CashEdiMain implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected CashEdiMainPK cashEdiMainPK;
@Basic(optional = false)
@Column(name = "ABSENDER", nullable = false, length = 13)
private String absender;
@Basic(optional = false)
@Column(name = "EMPFAENGER", nullable = false, length = 13)
private String empfaenger;
@Basic(optional = false)
@Column(name = "ERSTELLDATUM", nullable = false)
@Temporal(TemporalType.DATE)
private Date erstelldatum;
@Column(name = "EINZAHLUNGSART")
private int einzahlungsart;
@Column(name = "NRT_SENDER", length = 13)
private String nrtSender;
@Column(name = "NRT_EMPFAENDER", length = 13)
private String nrtEmpfaender;
@Column(name = "BUNDESBANKFILIALE", length = 13)
private String bundesbankfiliale;
@Column(name = "TRANSPORTEUR", length = 13)
private String transporteur;
@Column(name = "ANZAHL_GESAMT")
private int anzahlGesamt;
@Column(name = "ABZ_KUNDE")
@Temporal(TemporalType.DATE)
private Date abzKunde;
@Column(name = "VERWENDUNGSZWECK", length = 140)
private String verwendungszweck;
@Column(name = "KD_REF", length = 70)
private String kdRef;
@Column(name = "EINZAHLER_BMS")
private Long einzahlerBms;
@Column(name = "FERTIGER", length = 13)
private String fertiger;
@Column(name = "SUMME", precision = 11, scale = 2)
private Float summe;
@Column(name = "BELEGNR", length = 50)
private String belegnr;
@Column(name = "ERLEDIGT")
private Character erledigt;
@Column(name = "ERLEDIGT_DATUM")
@Temporal(TemporalType.DATE)
private Date erledigtDatum;
@Column(name = "EAN_TEXT", length = 4000)
private String eanText;
@Column(name = "ERLEDIGT_DATEI", length = 44)
private String erledigtDatei;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "cashEdiMain")
private List<CashEdiPosition> cashEdiPositionList;
public CashEdiMain() {
}
public CashEdiMain(CashEdiMainPK cashEdiMainPK) {
this.cashEdiMainPK = cashEdiMainPK;
}
public CashEdiMain(CashEdiMainPK cashEdiMainPK, String absender, String empfaenger, Date erstelldatum) {
this.cashEdiMainPK = cashEdiMainPK;
this.absender = absender;
this.empfaenger = empfaenger;
this.erstelldatum = erstelldatum;
}
public CashEdiMain(String dateiId, String lieferNve) {
this.cashEdiMainPK = new CashEdiMainPK(dateiId, lieferNve);
}
public CashEdiMainPK getCashEdiMainPK() {
return cashEdiMainPK;
}
public void setCashEdiMainPK(CashEdiMainPK cashEdiMainPK) {
this.cashEdiMainPK = cashEdiMainPK;
}
public String getAbsender() {
return absender;
}
public void setAbsender(String absender) {
this.absender = absender;
}
public String getEmpfaenger() {
return empfaenger;
}
public void setEmpfaenger(String empfaenger) {
this.empfaenger = empfaenger;
}
public Date getErstelldatum() {
return erstelldatum;
}
public void setErstelldatum(Date erstelldatum) {
this.erstelldatum = erstelldatum;
}
public int getEinzahlungsart() {
return einzahlungsart;
}
public void setEinzahlungsart(int einzahlungsart) {
this.einzahlungsart = einzahlungsart;
}
public String getNrtSender() {
return nrtSender;
}
public void setNrtSender(String nrtSender) {
this.nrtSender = nrtSender;
}
public String getNrtEmpfaender() {
return nrtEmpfaender;
}
public void setNrtEmpfaender(String nrtEmpfaender) {
this.nrtEmpfaender = nrtEmpfaender;
}
public String getBundesbankfiliale() {
return bundesbankfiliale;
}
public void setBundesbankfiliale(String bundesbankfiliale) {
this.bundesbankfiliale = bundesbankfiliale;
}
public String getTransporteur() {
return transporteur;
}
public void setTransporteur(String transporteur) {
this.transporteur = transporteur;
}
public int getAnzahlGesamt() {
return anzahlGesamt;
}
public void setAnzahlGesamt(int anzahlGesamt) {
this.anzahlGesamt = anzahlGesamt;
}
public Date getAbzKunde() {
return abzKunde;
}
public void setAbzKunde(Date abzKunde) {
this.abzKunde = abzKunde;
}
public String getVerwendungszweck() {
return verwendungszweck;
}
public void setVerwendungszweck(String verwendungszweck) {
this.verwendungszweck = verwendungszweck;
}
public String getKdRef() {
return kdRef;
}
public void setKdRef(String kdRef) {
this.kdRef = kdRef;
}
public Long getEinzahlerBms() {
return einzahlerBms;
}
public void setEinzahlerBms(Long einzahlerBms) {
this.einzahlerBms = einzahlerBms;
}
public String getFertiger() {
return fertiger;
}
public void setFertiger(String fertiger) {
this.fertiger = fertiger;
}
public Float getSumme() {
return summe;
}
public void setSumme(Float summe) {
this.summe = summe;
}
public String getBelegnr() {
return belegnr;
}
public void setBelegnr(String belegnr) {
this.belegnr = belegnr;
}
public Character getErledigt() {
return erledigt;
}
public void setErledigt(Character erledigt) {
this.erledigt = erledigt;
}
public Date getErledigtDatum() {
return erledigtDatum;
}
public void setErledigtDatum(Date erledigtDatum) {
this.erledigtDatum = erledigtDatum;
}
public String getEanText() {
return eanText;
}
public void setEanText(String eanText) {
this.eanText = eanText;
}
public String getErledigtDatei() {
return erledigtDatei;
}
public void setErledigtDatei(String erledigtDatei) {
this.erledigtDatei = erledigtDatei;
}
public List<CashEdiPosition> getCashEdiPositionList() {
return cashEdiPositionList;
}
public void setCashEdiPositionList(List<CashEdiPosition> cashEdiPositionList) {
this.cashEdiPositionList = cashEdiPositionList;
}
@Override
public int hashCode() {
int hash = 0;
hash += (cashEdiMainPK != null ? cashEdiMainPK.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof CashEdiMain)) {
return false;
}
CashEdiMain other = (CashEdiMain) object;
if ((this.cashEdiMainPK == null && other.cashEdiMainPK != null) || (this.cashEdiMainPK != null && !this.cashEdiMainPK.equals(other.cashEdiMainPK))) {
return false;
}
return true;
}
Code:
public class CashEdiPosition implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
protected CashEdiPositionPK cashEdiPositionPK;
@Column(name = "ARTIKEL_ID", length = 13)
private String artikelId;
@Column(name = "BETRAG", precision = 11, scale = 2)
private Float betrag;
@Column(name = "MENGE")
private int menge;
@Column(name = "PACKSTUECK_NVE", length = 18)
private String packstueckNve;
@Column(name = "PACKSTUECK_ID", length = 2)
private String packstueckId;
@JoinColumns({
@JoinColumn(name = "DATEI_ID", referencedColumnName = "DATEI_ID", nullable = false, insertable = false, updatable = false),
@JoinColumn(name = "LIEFER_NVE", referencedColumnName = "LIEFER_NVE", nullable = false, insertable = false, updatable = false)})
@ManyToOne(optional = false)
private CashEdiMain cashEdiMain;
public CashEdiPosition() {
}
public CashEdiPosition(CashEdiPositionPK cashEdiPositionPK) {
this.cashEdiPositionPK = cashEdiPositionPK;
}
public CashEdiPosition(String dateiId, int positionsNr, String lieferNve) {
this.cashEdiPositionPK = new CashEdiPositionPK(dateiId, positionsNr, lieferNve);
}
public CashEdiPositionPK getCashEdiPositionPK() {
return cashEdiPositionPK;
}
public void setCashEdiPositionPK(CashEdiPositionPK cashEdiPositionPK) {
this.cashEdiPositionPK = cashEdiPositionPK;
}
public String getArtikelId() {
return artikelId;
}
public void setArtikelId(String artikelId) {
this.artikelId = artikelId;
}
public Float getBetrag() {
return betrag;
}
public void setBetrag(Float betrag) {
this.betrag = betrag;
}
public int getMenge() {
return menge;
}
public void setMenge(int menge) {
this.menge = menge;
}
public String getPackstueckNve() {
return packstueckNve;
}
public void setPackstueckNve(String packstueckNve) {
this.packstueckNve = packstueckNve;
}
public String getPackstueckId() {
return packstueckId;
}
public void setPackstueckId(String packstueckId) {
this.packstueckId = packstueckId;
}
public CashEdiMain getCashEdiMain() {
return cashEdiMain;
}
public void setCashEdiMain(CashEdiMain cashEdiMain) {
this.cashEdiMain = cashEdiMain;
}
@Override
public int hashCode() {
int hash = 0;
hash += (cashEdiPositionPK != null ? cashEdiPositionPK.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof CashEdiPosition)) {
return false;
}
CashEdiPosition other = (CashEdiPosition) object;
if ((this.cashEdiPositionPK == null && other.cashEdiPositionPK != null) || (this.cashEdiPositionPK != null && !this.cashEdiPositionPK.equals(other.cashEdiPositionPK))) {
return false;
}
return true;
}
Code:
@Embeddable
public class CashEdiMainPK implements Serializable {
@Basic(optional = false)
@Column(name = "DATEI_ID", nullable = false, length = 18)
private String dateiId;
@Basic(optional = false)
@Column(name = "LIEFER_NVE", nullable = false, length = 18)
private String lieferNve;
public CashEdiMainPK() {
}
public CashEdiMainPK(String dateiId, String lieferNve) {
this.dateiId = dateiId;
this.lieferNve = lieferNve;
}
public String getDateiId() {
return dateiId;
}
public void setDateiId(String dateiId) {
this.dateiId = dateiId;
}
public String getLieferNve() {
return lieferNve;
}
public void setLieferNve(String lieferNve) {
this.lieferNve = lieferNve;
}
@Override
public int hashCode() {
int hash = 0;
hash += (dateiId != null ? dateiId.hashCode() : 0);
hash += (lieferNve != null ? lieferNve.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof CashEdiMainPK)) {
return false;
}
CashEdiMainPK other = (CashEdiMainPK) object;
if ((this.dateiId == null && other.dateiId != null) || (this.dateiId != null && !this.dateiId.equals(other.dateiId))) {
return false;
}
if ((this.lieferNve == null && other.lieferNve != null) || (this.lieferNve != null && !this.lieferNve.equals(other.lieferNve))) {
return false;
}
return true;
}
Code:
@Embeddable
public class CashEdiPositionPK implements Serializable {
@Basic(optional = false)
@Column(name = "DATEI_ID", nullable = false, length = 18)
private String dateiId;
@Basic(optional = false)
@Column(name = "POSITIONS_NR", nullable = false)
private int positionsNr;
@Basic(optional = false)
@Column(name = "LIEFER_NVE", nullable = false, length = 18)
private String lieferNve;
public CashEdiPositionPK() {
}
public CashEdiPositionPK(String dateiId, int positionsNr, String lieferNve) {
this.dateiId = dateiId;
this.positionsNr = positionsNr;
this.lieferNve = lieferNve;
}
public String getDateiId() {
return dateiId;
}
public void setDateiId(String dateiId) {
this.dateiId = dateiId;
}
public int getPositionsNr() {
return positionsNr;
}
public void setPositionsNr(int positionsNr) {
this.positionsNr = positionsNr;
}
public String getLieferNve() {
return lieferNve;
}
public void setLieferNve(String lieferNve) {
this.lieferNve = lieferNve;
}
@Override
public int hashCode() {
int hash = 0;
hash += (dateiId != null ? dateiId.hashCode() : 0);
hash += (int) positionsNr;
hash += (lieferNve != null ? lieferNve.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof CashEdiPositionPK)) {
return false;
}
CashEdiPositionPK other = (CashEdiPositionPK) object;
if ((this.dateiId == null && other.dateiId != null) || (this.dateiId != null && !this.dateiId.equals(other.dateiId))) {
return false;
}
if (this.positionsNr != other.positionsNr) {
return false;
}
if ((this.lieferNve == null && other.lieferNve != null) || (this.lieferNve != null && !this.lieferNve.equals(other.lieferNve))) {
return false;
}
return true;
}