Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA JOINTABLE MANYTOMANY JointColumn非主LazyInitializationDuplicate上的异常_Java_Hibernate_Jpa_Jdbc_Dao - Fatal编程技术网

Java JPA JOINTABLE MANYTOMANY JointColumn非主LazyInitializationDuplicate上的异常

Java JPA JOINTABLE MANYTOMANY JointColumn非主LazyInitializationDuplicate上的异常,java,hibernate,jpa,jdbc,dao,Java,Hibernate,Jpa,Jdbc,Dao,我有一个Hibernate实体对象类,用于带有列的表deviceinfo 设备信息 { id(主要), 设备驱动程序, 地位 名称 } Java Hibernate Entity as follows @XmlRootElement(name = "device") @Entity @Table(name="deviceinfo") public class DeviceInfo implements Serializable, Comparable<DeviceInfo > {

我有一个Hibernate实体对象类,用于带有列的表deviceinfo 设备信息 { id(主要), 设备驱动程序, 地位 名称 }

Java Hibernate Entity as follows
@XmlRootElement(name = "device")
@Entity
@Table(name="deviceinfo")
public class DeviceInfo implements Serializable, Comparable<DeviceInfo > {
 @ManyToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE})
 @JoinTable(
                name="VehicleDeviceCar",
            joinColumns={@JoinColumn(name="devicecarid", referencedColumnName="devicecarid", unique=false, insertable = false, updatable = false)},
            inverseJoinColumns={@JoinColumn(name="vehicleId", referencedColumnName="vehicleId", insertable = false, updatable = false)}
    )
    @XmlElement(name="vehicles")
    public Set<Vehicle> getVehicles() {
        return m_vehicles;
    }
}
魔鬼阶级:

    @XmlRootElement(name = "devices")
public class DeviceList  extends LinkedList<DeviceInfo> {

    private static final long serialVersionUID = 1L;    
    private int m_totalCount;

    public DeviceList() {
        super();
    }


    public DeviceList(Collection<? extends DeviceInfo> c) {
        super(c);
    }

    @XmlElement(name = "device")
    public List<DeviceInfo> getDevices() {
        return this;
    }

    public void setDevices(List<DeviceInfo> devices) {
        if (devices == this) return;
        clear();
        addAll(devices);
    }

    @XmlAttribute(name="count")
    public int getCount() {
        return this.size();
    }

    // The property has a getter "" but no setter. For unmarshalling, please define setters.
    public void setCount(final int count) {
    }

    @XmlAttribute(name="totalCount")
    public int getTotalCount() {
        return m_totalCount;
    }

    /**
     * <p>setTotalCount</p>
     *
     * @param count a int.
     */
    public void setTotalCount(int count) {
        m_totalCount = count;
    }
}
车辆

id  |   vehicleId   | vehicleName
1       |   V001            |   Truck1
车载设备

vehicleId   |   devicecarid
V001            |   1234
V001            |   1235
当我使用上面的rest调用时,我可以看到xml格式的输出

<devices count="1" totalCount="0">
  <device devicecarid="1234" id="1" status="true" name="car1234">
    <vehicles name="Truck1" id="1" vehicleId="V001"/>
  </device>
  <device devicecarid="1235" id="2" status="true" name="car1235">
    <vehicles name="Truck1" id="1" vehicleId="V001"/>
  </device>
</devices>
例外情况如下:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.opennms.netmgt.model.OnmsWcru.trains, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
环境: JPA2.0 休眠3.6.10 JDBC9.1 PostgreSQL 9.2 Java 7


有人能给我指出正确的方向吗我哪里做错了

显示devicelist的源代码我已经用devicelist类更新了帖子..@shazin
<devices count="1" totalCount="0">
  <device devicecarid="1234" id="1" status="true" name="car1234">
    <vehicles name="Truck1" id="1" vehicleId="V001"/>
  </device>
  <device devicecarid="1235" id="2" status="true" name="car1235">
    <vehicles name="Truck1" id="1" vehicleId="V001"/>
  </device>
</devices>
id  |   devicecarid |   status  |   name
1       |   1234                |   true        | car1234
2       |   1235                |   true        | car1235
3       |   1235                |   false       | car1235
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.opennms.netmgt.model.OnmsWcru.trains, no session or session was closed
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]
    at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186) ~[hibernate-core-3.6.10.Final.jar:3.6.10.Final]