cancel
Showing results for 
Search instead for 
Did you mean: 

Tutorial of JPA one-to-many persist

Former Member
0 Kudos

Hello everyone,

     I have read the official document of Oracle, but I still have some problems about JPA relationships. Suppose that there are two tables: the header table and the item table.  The DDL are following:

   

CREATE TABLE TMP_ZSTEST_HEADER (
  MANDT VARCHAR(3) NOT NULL,
  BUKRS VARCHAR(4) NOT NULL,
  BELNR VARCHAR(10) NOT NULL,
  GJAHR VARCHAR(4) NOT NULL,
  BUTXT VARCHAR(20) NOT NULL,
  WAERS VARCHAR(5) NOT NULL,
  TCODE VARCHAR(20) NOT NULL,
  CONSTRAINT PrimaryKey_Header PRIMARY KEY (MANDT, BUKRS, BELNR, GJAHR)
);

CREATE TABLE TMP_ZSTEST_ITEM (
  MANDT VARCHAR(3) NOT NULL,
  BUKRS VARCHAR(4) NOT NULL,
  BELNR VARCHAR(10) NOT NULL,
  GJAHR VARCHAR(4) NOT NULL,
  BUZEI INTEGER DEFAULT 0 NOT NULL,
  DMBTR DECIMAL(13 , 2) DEFAULT 0 NOT NULL,
  COSTL VARCHAR(10) NOT NULL,
  ZUONR VARCHAR(20) NOT NULL,
  CONSTRAINT PrimaryKey_Item PRIMARY KEY (MANDT, BUKRS, BELNR, GJAHR, BUZEI),
  CONSTRAINT Foreignkey_Header_Item FOREIGN KEY (MANDT,BUKRS,BELNR,GJAHR) REFERENCES TMP_ZSTEST_HEADER(MANDT,BUKRS,BELNR,GJAHR)
);

The Entity Classes are following:

1. TmpZstestHeader.java

package com.sap.entities;

import java.io.Serializable;
import javax.persistence.*;
import java.util.List;


/**
* The persistent class for the TMP_ZSTEST_HEADER database table.
*
*/
@Entity
@Table(name="TMP_ZSTEST_HEADER")
public class TmpZstestHeader implements Serializable {
private static final long serialVersionUID = 1L;

@EmbeddedId
private TmpZstestHeaderPK id;

private String butxt;

private String tcode;

private String waers;

//bi-directional many-to-one association to TmpZstestItem
@OneToMany(mappedBy="tmpZstestHeader", cascade={CascadeType.ALL})
private List<TmpZstestItem> tmpZstestItems;

    public TmpZstestHeader() {
    }

public TmpZstestHeaderPK getId() {
  return this.id;
}

public void setId(TmpZstestHeaderPK id) {
  this.id = id;
}

public String getButxt() {
  return this.butxt;
}

public void setButxt(String butxt) {
  this.butxt = butxt;
}

public String getTcode() {
  return this.tcode;
}

public void setTcode(String tcode) {
  this.tcode = tcode;
}

public String getWaers() {
  return this.waers;
}

public void setWaers(String waers) {
  this.waers = waers;
}

public List<TmpZstestItem> getTmpZstestItems() {
  return this.tmpZstestItems;
}

public void setTmpZstestItems(List<TmpZstestItem> tmpZstestItems) {
  this.tmpZstestItems = tmpZstestItems;
}

}

2. TmpZstestHeaderPK.java

package com.sap.entities;

import java.io.Serializable;
import javax.persistence.*;

/**
* The primary key class for the TMP_ZSTEST_HEADER database table.
*
*/
@Embeddable
public class TmpZstestHeaderPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;

private String mandt;

private String bukrs;

private String belnr;

private String gjahr;

    public TmpZstestHeaderPK() {
    }
public String getMandt() {
  return this.mandt;
}
public void setMandt(String mandt) {
  this.mandt = mandt;
}
public String getBukrs() {
  return this.bukrs;
}
public void setBukrs(String bukrs) {
  this.bukrs = bukrs;
}
public String getBelnr() {
  return this.belnr;
}
public void setBelnr(String belnr) {
  this.belnr = belnr;
}
public String getGjahr() {
  return this.gjahr;
}
public void setGjahr(String gjahr) {
  this.gjahr = gjahr;
}

public boolean equals(Object other) {
  if (this == other) {
   return true;
  }
  if (!(other instanceof TmpZstestHeaderPK)) {
   return false;
  }
  TmpZstestHeaderPK castOther = (TmpZstestHeaderPK)other;
  return
   this.mandt.equals(castOther.mandt)
   && this.bukrs.equals(castOther.bukrs)
   && this.belnr.equals(castOther.belnr)
   && this.gjahr.equals(castOther.gjahr);

    }
   
public int hashCode() {
  final int prime = 31;
  int hash = 17;
  hash = hash * prime + this.mandt.hashCode();
  hash = hash * prime + this.bukrs.hashCode();
  hash = hash * prime + this.belnr.hashCode();
  hash = hash * prime + this.gjahr.hashCode();
 
  return hash;
    }
}

3. TmpZstestItem.java

package com.sap.entities;

import java.io.Serializable;
import javax.persistence.*;
import java.math.BigDecimal;


/**
* The persistent class for the TMP_ZSTEST_ITEM database table.
*
*/
@Entity
@Table(name="TMP_ZSTEST_ITEM")
public class TmpZstestItem implements Serializable {
private static final long serialVersionUID = 1L;

@EmbeddedId
private TmpZstestItemPK id;

private String costl;

private BigDecimal dmbtr;

private String zuonr;

//bi-directional many-to-one association to TmpZstestHeader
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumns({
  @JoinColumn(name="BELNR", referencedColumnName="BELNR"),
  @JoinColumn(name="BUKRS", referencedColumnName="BUKRS"),
  @JoinColumn(name="GJAHR", referencedColumnName="GJAHR"),
  @JoinColumn(name="MANDT", referencedColumnName="MANDT")
  })
private TmpZstestHeader tmpZstestHeader;

    public TmpZstestItem() {
    }

public TmpZstestItemPK getId() {
  return this.id;
}

public void setId(TmpZstestItemPK id) {
  this.id = id;
}

public String getCostl() {
  return this.costl;
}

public void setCostl(String costl) {
  this.costl = costl;
}

public BigDecimal getDmbtr() {
  return this.dmbtr;
}

public void setDmbtr(BigDecimal dmbtr) {
  this.dmbtr = dmbtr;
}

public String getZuonr() {
  return this.zuonr;
}

public void setZuonr(String zuonr) {
  this.zuonr = zuonr;
}

public TmpZstestHeader getTmpZstestHeader() {
  return this.tmpZstestHeader;
}

public void setTmpZstestHeader(TmpZstestHeader tmpZstestHeader) {
  this.tmpZstestHeader = tmpZstestHeader;
}

}

4. TmpZstestItemPK.java

package com.sap.entities;

import java.io.Serializable;
import javax.persistence.*;

/**
* The primary key class for the TMP_ZSTEST_ITEM database table.
*
*/
@Embeddable
public class TmpZstestItemPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;

private String mandt;

private String bukrs;

private String belnr;

private String gjahr;

private int buzei;

    public TmpZstestItemPK() {
    }
public String getMandt() {
  return this.mandt;
}
public void setMandt(String mandt) {
  this.mandt = mandt;
}
public String getBukrs() {
  return this.bukrs;
}
public void setBukrs(String bukrs) {
  this.bukrs = bukrs;
}
public String getBelnr() {
  return this.belnr;
}
public void setBelnr(String belnr) {
  this.belnr = belnr;
}
public String getGjahr() {
  return this.gjahr;
}
public void setGjahr(String gjahr) {
  this.gjahr = gjahr;
}
public int getBuzei() {
  return this.buzei;
}
public void setBuzei(int buzei) {
  this.buzei = buzei;
}

public boolean equals(Object other) {
  if (this == other) {
   return true;
  }
  if (!(other instanceof TmpZstestItemPK)) {
   return false;
  }
  TmpZstestItemPK castOther = (TmpZstestItemPK)other;
  return
   this.mandt.equals(castOther.mandt)
   && this.bukrs.equals(castOther.bukrs)
   && this.belnr.equals(castOther.belnr)
   && this.gjahr.equals(castOther.gjahr)
   && (this.buzei == castOther.buzei);

    }
   
public int hashCode() {
  final int prime = 31;
  int hash = 17;
  hash = hash * prime + this.mandt.hashCode();
  hash = hash * prime + this.bukrs.hashCode();
  hash = hash * prime + this.belnr.hashCode();
  hash = hash * prime + this.gjahr.hashCode();
  hash = hash * prime + this.buzei;
 
  return hash;
    }
}

My Session Class is following:

Session.java

package com.sap.session;

import javax.ejb.Stateless;
import javax.persistence.*;

import com.sap.entities.*;

/**
* Session Bean implementation class Session
*/
@Stateless
public class Session implements SessionLocal {
    @PersistenceUnit(unitName = "unitName")
private EntityManagerFactory emf;
private EntityManager em;
    /**
     * Default constructor.
     */
    public Session() {
        // TODO Auto-generated constructor stub
    }
   
    public void AddFromHeader(TmpZstestHeader entity){
     em = emf.createEntityManager();
     em.persist(entity);
     em.flush();
    }
   
    public void AddFromItem(TmpZstestItem entity){
     em = emf.createEntityManager();
     em.persist(entity);
     em.flush();
    }
}

But when I run the method AddFromHeader, the exception indicates that the four columns MANDT, BUKRS, BELNR, GJAHR are not unique.

How can I do if I want to create entities in table TMP_ZSTEST_ITEM when I persist the entity of TMP_ZSTEST_HEADER?

Is there any tutorial about this?

Regards,

Zhongsheng Xu

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Can you post the exception? Bit hard to read all the code there - the exception should hopefully narrow down the problem.

Former Member
0 Kudos

Thanks for your reply!

The following exception always occurs:

(Failed in component: sap.com/tc~ejbexplorer~ear, BC-JAS-EJB) nested exception is: com.sap.engine.services.ts.exceptions.BaseRollbackException: Exception in beforeCompletion of ( JTA Transaction : 86692 ).

Could you tell me, what is JTA Transaction : 86692, and what does each transaction number correspond to? 

Former Member
0 Kudos

That refers to the current database transaction being run (ie the operation you are running on the database). It is being rollback, so no changes are made. However this doesn't show the actual error, is there any more of the exception?

Former Member
0 Kudos

I tested it in the EJB Explorer and I have checked the log, the log doesn't show the actual error either.

Maybe I can only know the actual error until I test it in webdynpro.

Former Member
0 Kudos

Have you tried in netweaver administrator, "Troubleshooting > Logs and Traces > Log Viewer"

Then set the view to developer by going to "View > Open View > Developer Traces. Usually has a bit more info there. There may be 3 or so entries for an EJB with JPA so you may have to click the expand button on all three to get the right error message. The errors will show up towards the bottom of the stack trace.

Former Member
0 Kudos

Thank you very much!

When I test the EJB method in Webdynpro, the data has been inserted into the database correctly. But I still can not understand the reasons why the error occurs in EJB explorer while it doesn't occur in Webdynpro.

former_member192152
Active Participant
0 Kudos

Check the link (http://www.filedropper.com/persistenciaejbbpm) content helps you in any way.

Best regards,

Angelo