簡體   English   中英

一對多關系Java JPA的問題

[英]Problems with One to many relationship Java JPA

我的JPA有問題。 基本上我的Couriers是由程序創建的,客戶和包裹是由用戶在運行時創建的。 當我嘗試添加新宗地時,我將對象添加到Courier中的客戶和宗地列表中的宗地列表中。 但是當它試圖添加到Courier包裹列表時它會崩潰。 我在主類中調用菜單之前創建了一個快遞對象

並且它拋出以下錯誤:在同步期間,通過未標記為級聯的關系找到新對象PERSIST:Courier:Id:0名稱:null Vehicle: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;
}

// CUSTOMER CLASS

    @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;
}

}

//添加新宗地的JPA方法

    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;
    }

}

您正在將包裹添加到尚未在數據庫中保留Courier中。

你必須堅持你的Courier對象。

既然你告訴你的Courier對象它應該級聯持有它的Parcel對象,它實際上應該足以保持Courer而不是自己持久保存每個parcel:

Parcel parcel;
Customer c = em.find(Customer.class, id);
Courier co = new Courier(); 
em.getTransaction().begin();
if(type.equals("INT")){
    parcel = new InternationalParcel(heightAdd, widthAdd, lengthAdd, weightAdd, receiverAdd);
} else {
    parcel = new NationalParcel(heightAdd, widthAdd, lengthAdd, weightAdd,receiverAdd);
}
co.addParcel(parcel);
em.persist(co); // <- This is what you are currently not doing! 
em.persist(parcel); // <- this might not be necessary because of cascade persist
c.addParcel(parcel);
em.getTransaction().commit();
return parcel;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM