复制时JPA JOINTABLE MANYTOMANY JOINCOLUMN非主要LazyInitializationException

时间:2014-06-14 09:30:24

标签: java hibernate jpa jdbc dao

我有一个带有列的表deviceinfo的Hibernate实体对象类     的 deviceinfo     {         ID(主),         devicecarid,         状态,         名称     }

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

另一个hibernate实体Object Class,用于带有列的表车辆     的车辆     {         ID,         vehicleId(主),         vehicleName     }

另一个名为VehicleDeviceCar的表,其中包含列     的 VehicleDeviceCar     {         vehicleId(车辆的外键),         devicecarid     } 此表没有任何主键。

Therse是一个RESET服务servlet,用于获取deviceinfo,如下所示

@Component
@PerRequest
@Scope("prototype")
@Path("deviceInfo")
@Transactional
public class DeviceRestService extends RestService{
    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.APPLICATION_ATOM_XML})
    public DeviceList getDeviceInfo(){
        readLock();     
        try{
            return new DeviceList(m_deviceDao.findAll());
        }finally {
            readUnlock();
        }
    }
}

DeviceList类:

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

使用下表数据:

deviceinfo

id  |   devicecarid |   status  |   name
1       |   1234                |   true        | car1234
2       |   1235                |   true        | car1235

车辆

id  |   vehicleId   | vehicleName
1       |   V001            |   Truck1

VehicleDeviceCar

vehicleId   |   devicecarid
V001            |   1234
V001            |   1235

当我使用上面的休息调用时,我可以看到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>

当deviceinfo表中存在重复项时,抛出LazyInitialization异常,例如, deviceinfo

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]

环境: JPA 2.0 Hibernate 3.6.10 JDBC 9.1 PostgreSQL 9.2 Java 7

有人可以指出我正确的方向我在哪里做错了。

0 个答案:

没有答案