-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: JOINTABLE MANYTOMANY JOINCOLUMN non primary LazyInitializati
PostPosted: Sun Jun 15, 2014 10:08 pm 
Newbie

Joined: Sun Jun 15, 2014 10:02 pm
Posts: 1
I have an Hibernate Entity Object Class for the table deviceinfo with columns
**deviceinfo
{
id(primary),
devicecarid,
status,
name
}**

Java Hibernate Entity as follows
Code:
@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;
        }
    }


Another hibernate entity Object Class called Vehicle for table vehicles with columns
**vehicles
{
id,
vehicleId(primary),
vehicleName
}**

Another table called VehicleDeviceCar with columns
**VehicleDeviceCar
{
vehicleId(Foreign key to vehicles),
devicecarid
}**
**This table doesn't have any primary key.**

Therse is a RESET service servlet written to fetch the deviceinfo as follows

Code:
@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 class:

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


With the below table data :

deviceinfo

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

vehicles

id | vehicleId | vehicleName
1 | V001 | Truck1

VehicleDeviceCar

vehicleId | devicecarid
V001 | 1234
V001 | 1235

When I use the above rest call I can see the output in xml format

Code:
<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>


LazyInitialization exception is thrown when there are duplicates in deviceinfo table e.g.,
deviceinfo

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

Exception is as follows:

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]

Environment:
**JPA 2.0
Hibernate 3.6.10
JDBC 9.1
PostgreSQL 9.2
Java 7**

Could some one please point me in right direction where am I doing wrong ..


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.