org.hibernate.PersistentObjectException:传递给persist的分离实体:com.nividous.models.Vehicleowner_Cascade

时间:2015-01-20 12:01:17

标签: java hibernate one-to-many cascade many-to-one

我正在尝试使用Cascade类型但在这里得到错误我的bean类是。 一对多映射类

@Entity
@Table(name = "VEHICLE_USER_CascadeType_Presist")
public class Vehicleowner_Cascade {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int vuserid;
    private String vusername;

    @OneToMany(mappedBy = "vehicleowner", cascade = CascadeType.PERSIST)
    private Collection<Vehicle_object_Cascade> vehicle_objects = new ArrayList<Vehicle_object_Cascade>();

    public Vehicleowner_Cascade() {
        super();

    }

    public Vehicleowner_Cascade(int vuserid, String vusername) {
        super();
        this.vuserid = vuserid;
        this.vusername = vusername;
    }

    public Vehicleowner_Cascade(int vuserid, String vusername,
            Collection<Vehicle_object_Cascade> vehicle_objects) {
        super();
        this.vuserid = vuserid;
        this.vusername = vusername;
        this.vehicle_objects = vehicle_objects;
    }

    public int getVuserid() {
        return vuserid;
    }

    public void setVuserid(int vuserid) {
        this.vuserid = vuserid;
    }

    public String getVusername() {
        return vusername;
    }

    public void setVusername(String vusername) {
        this.vusername = vusername;
    }

    public Collection<Vehicle_object_Cascade> getVehicle_objects() {
        return vehicle_objects;
    }

    public void setVehicle_objects(
            Collection<Vehicle_object_Cascade> vehicle_objects) {
        this.vehicle_objects = vehicle_objects;
    }

}

多对一

@Entity
public class Vehicle_object_Cascade {
    @Id
    @GeneratedValue
    private int vehicle_id;
    private String vehicle_name;


    @ManyToOne
    @NotFound(action = NotFoundAction.IGNORE)
    private Vehicleowner_Cascade vehicleowner;

    public Vehicle_object_Cascade() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Vehicleowner_Cascade getVehicleowner() {
        return vehicleowner;
    }

    public void setVehicleowner(Vehicleowner_Cascade vehicleowner) {
        this.vehicleowner = vehicleowner;
    }

    public Vehicle_object_Cascade(int vehicle_id, String vehicle_name,
            Vehicleowner_Cascade vehicleowner) {
        super();
        this.vehicle_id = vehicle_id;
        this.vehicle_name = vehicle_name;
        this.vehicleowner = vehicleowner;
    }

    public Vehicle_object_Cascade(int vehicle_id, String vehicle_name) {
        super();
        this.vehicle_id = vehicle_id;
        this.vehicle_name = vehicle_name;
    }

    public int getVehicle_id() {
        return vehicle_id;
    }

    public void setVehicle_id(int vehicle_id) {
        this.vehicle_id = vehicle_id;
    }

    public String getVehicle_name() {
        return vehicle_name;
    }

    public void setVehicle_name(String vehicle_name) {
        this.vehicle_name = vehicle_name;
    }

}

主要课程是

try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();

            Vehicle_object_Cascade vehicle1 = new Vehicle_object_Cascade(1,
                    "Activa");
            // vehicle1.setVehicle_name("Activa");
            Vehicle_object_Cascade vehicle2 = new Vehicle_object_Cascade(2,
                    "Shine");
            // vehicle2.setVehicle_name("Shine");
            Vehicle_object_Cascade vehicle3 = new Vehicle_object_Cascade(3,
                    "Apache");
            // vehicle3.setVehicle_name("Apache");
            Vehicle_object_Cascade vehicle4 = new Vehicle_object_Cascade(4,
                    "Neo");
            // vehicle4.setVehicle_name("Neo");

            Vehicleowner_Cascade vehicleowner1 = new Vehicleowner_Cascade(1,
                    "Krishna");
            Vehicleowner_Cascade vehicleowner2 = new Vehicleowner_Cascade(2,
                    "Dhaval");

            vehicleowner1.getVehicle_objects().add(vehicle1);
            vehicleowner1.getVehicle_objects().add(vehicle2);
            vehicleowner2.getVehicle_objects().add(vehicle3);
            vehicleowner2.getVehicle_objects().add(vehicle4);

            vehicle1.setVehicleowner(vehicleowner1);
            vehicle1.setVehicleowner(vehicleowner1);
            vehicle3.setVehicleowner(vehicleowner2);
            vehicle4.setVehicleowner(vehicleowner2);

            session.persist(vehicleowner1);
            session.persist(vehicleowner2);

            session.getTransaction().commit();
            session.clear();
            session.close();

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

但是我收到了这个错误。

org.hibernate.PersistentObjectException: detached entity passed to persist: com.nividous.models.Vehicleowner_Cascade
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:141)
    at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:78)
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:772)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:746)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:750)
    at com.nivious.Dao.DaoAcccessClass.insert_vehicle(DaoAcccessClass.java:157)
    at com.nividous.Controller.MainClass_OnetoOneTesting.main(MainClass_OnetoOneTesting.java:8)

任何人都可以帮助我。提前谢谢。

0 个答案:

没有答案