一对多关系的问题Java JPA

时间:2016-11-25 16:24:10

标签: java jpa

我的JPA有问题。 基本上我的Couriers是由程序创建的,客户和包裹是由用户在运行时创建的。当我尝试添加新宗地时,我将对象添加到Courier中的客户和宗地列表中的宗地列表中。但是当它试图添加到Courier包裹列表时它会崩溃。我在主类

中调用菜单之前创建了一个快递对象

它会抛出以下错误: 在同步过程中,通过未标记为级联的关系找到了一个新对象PERSIST:Courier: Id: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;
}

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

}

1 个答案:

答案 0 :(得分:0)

您正在将 Parcel 添加到尚未保留在数据库中的 Courier

您还必须保留 Courier 对象。

既然你告诉你的Courier对象它应该级联持有它的Parcel对象,它实际上应该足以坚持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;