1
JPAに問題があります。 基本的に私のCourierはプログラムによって作成され、顧客と小包は実行中にユーザーによって作成されます。新しいパーセルを追加しようとすると、そのオブジェクトを顧客のパーセルリストに追加し、クーリエのパーセルリストに追加します。しかし、Courierのパーセルリストに追加しようとするとクラッシュします。私は、メインクラス1対多関係の問題Java JPA
で私のメニューを呼び出す前に、宅配便のオブジェクトを作成し、それが次のエラーがスローされます。同期中に は、新しいオブジェクトがカスケードをマークされていなかった関係を通じて発見されたPERSIST:宅配便: イド:0 名前:null 車両:null。
これは私のコードです:
@Entity
@SequenceGenerator(name="cou_seq", initialValue=1, allocationSize=1)
@SuppressWarnings("SerializableClass")
public class Courier implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="cou_seq")
private int couId;
private String name;
private String vehicle;
@OneToMany(mappedBy = "courier", cascade = CascadeType.PERSIST)
private List<Parcel> plist = new ArrayList<>();
public Courier(){
}
public Courier(String nameIn, String vehicleIn){
name = nameIn;
vehicle = vehicleIn;
}
public void addParcel(Parcel p1){
plist.add(p1);
p1.setCo(this);
}
public int getCouId() {
return couId;
}
public String getName() {
return name;
}
public String getVehicle() {
return vehicle;
}
public void setCouId(int couId) {
this.couId = couId;
}
public void setName(String name) {
this.name = name;
}
public void setVehicle(String vehicle) {
this.vehicle = vehicle;
}
public List<Parcel> getParcel(){
return plist;
}
public void setParcel(List<Parcel> parcels) {
plist = parcels;
}
@Override
public String toString(){
return "Courier: \nId: " + couId + "\nName: " + name + "\nVehicle: " + vehicle;
}
//顧客クラス
@Entity
@SequenceGenerator(name="cus_seq", initialValue=1, allocationSize=1)
@SuppressWarnings("SeralizableClass")
public class Customer implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="cus_seq")
private int cusId;
private String login;
private String password;
private String fname;
private String lname;
private String dob;
private String address;
private String phoneNo;
private String email;
@OneToMany(mappedBy = "customer")
private List<Parcel> plist = new ArrayList<>();
public Customer(){
}
public Customer(String loginIn, String passwordIn, String fnameIn, String lnameIn, String dobIn, String addressIn, String phoneNoIn, String emailIn){
login = loginIn;
password = passwordIn;
fname = fnameIn;
lname = lnameIn;
dob = dobIn;
address = addressIn;
phoneNo = phoneNoIn;
email = emailIn;
}
public void addParcel(Parcel p) {
plist.add(p);
p.setC(this);
}
public String getFname() {
return fname;
}
public String getLname() {
return lname;
}
public String getDob() {
return dob;
}
public String getAddress() {
return address;
}
public String getPhoneNo() {
return phoneNo;
}
public String getEmail() {
return email;
}
public void setFname(String fname) {
this.fname = fname;
}
public void setLname(String lname) {
this.lname = lname;
}
public void setDob(String dob) {
this.dob = dob;
}
public void setAddress(String address) {
this.address = address;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public void setEmail(String email) {
this.email = email;
}
public List<Parcel> getParcel(){
return plist;
}
public void setParcel(List<Parcel> parcels) {
plist = parcels;
}
public String toString(){
return "Customer: " + "\nID: " + cusId + "\nLogin: " + login + "\nFirst Name: " + fname + "\nSecond Name: " + lname + "\nDOB: " + dob + "\nAddress: " + address + "\nPhone No: " + phoneNo;
}
}
// PARCELのCLASS
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type")
@SequenceGenerator(name = "par_seq", initialValue = 1, allocationSize = 1)
@SuppressWarnings("SerializableClass")
public class Parcel {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "par_seq")
private int parId;
private double height;
private double width;
private double length;
private double weight;
private String receiver;
@ManyToOne()
@JoinColumn(name = "cuId")
private Customer customer;
@ManyToOne()
@JoinColumn(name = "coId")
private Courier courier;
private static double price;
public Parcel() {
}
public Parcel(double heightIn, double widthIn, double lengthIn, double weightIn, String receiverIn) {
height = heightIn;
width = widthIn;
length = lengthIn;
weight = weightIn;
receiver = receiverIn;
}
public double getHeight() {
return height;
}
public double getWidth() {
return width;
}
public double getLength() {
return length;
}
public double getWeight() {
return weight;
}
public double getPrice() {
return price;
}
public void setHeight(double height) {
this.height = height;
}
public void setWidth(double width) {
this.width = width;
}
public void setLength(double length) {
this.length = length;
}
public void setWeight(double weight) {
this.weight = weight;
}
public void setPrice(double price) {
this.price = price;
}
public double calcSize(double height, double width, double length) {
return height * width * length;
}
public void setC(Customer c) {
this.customer = c;
}
public Customer getC() {
return customer;
}
public void setCo(Courier c1) {
this.courier = c1;
}
public Courier getCo() {
return courier;
}
@Override
public String toString() {
return "Parcel:\nHeight: " + height + "\nWidth: " + width + "\nLength: " + length + "\nWeight: " + weight;
}
}
あなたはまだデータベースに永続化されていないクーリエにご小包を追加している
public Parcel createParcel(double heightAdd, double widthAdd, double lengthAdd, double weightAdd, String receiverAdd,String owner,String type){
int id = findCustomerIdByLogin(owner);
Customer c = em.find(Customer.class, id);
Courier co = new Courier();
em.getTransaction().begin();
if(type.equals("INT")){
System.out.println("Inside here");
InternationalParcel int1 = new InternationalParcel(heightAdd, widthAdd, lengthAdd, weightAdd, receiverAdd);
em.persist(int1);
c.addParcel(int1);
//em.persist(int1);
co.addParcel(int1);
em.getTransaction().commit();
return int1;
} else {
NationalParcel nat1 = new NationalParcel(heightAdd, widthAdd, lengthAdd, weightAdd,receiverAdd);
em.persist(nat1);
c.addParcel(nat1);
em.persist(nat1);
co.addParcel(nat1);
em.getTransaction().commit();
return nat1;
}
}
ようこそSOへ:あなたは、その小包は、実際には独自に各パーセルを持続させずにCourerを持続するために十分であるべきオブジェクトを永続化カスケード必要があることをあなたの宅配便オブジェクトを告げたので
!あなたのソースコードの字下げを修正して、あなたの質問の中核にしてください。これは多くのソースコードであり、あなたのソリューションに何が間違っているかを簡単に把握できるようになると、人々はあなたを助けてくれるでしょう。 –