2016-11-25 71 views
1

我有我的JPA的一个问题。 基本上我的信使是由程序创建的,客户和包裹是用户在运行时创建的。当我尝试添加新宗地时,将对象添加到客户中的宗地列表和Courier中的宗地列表中。但尝试添加到Courier包裹列表时它崩溃。我打电话给我的菜单中的主类问题与一对多的关系Java的JPA

之前创建一个快递对象,并引发以下错误: 在同步的新对象是通过没有标记级联关系找到PERSIST:快递: 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类

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

}

//包裹类

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

}

+1

欢迎进入SO!你能否修正你的源代码的缩进,并且可能将其解决到你的问题的核心?这是许多源代码可供阅读,如果他们更容易找出解决方案的问题,人们更可能帮助您。 –

回答

0

你加入你的包裹快递未在数据库坚持呢。

你必须坚持你的快递对象为好。

既然你说你的快递对象,它应该坚持级联的地块对象它实际上应该是不够的,只是坚持的Courer不会对自己的坚持每件包裹:

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;