-->
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.  [ 9 posts ] 
Author Message
 Post subject: Problem with 2 applications using JPA/Hiber and the same db
PostPosted: Mon Jul 23, 2007 5:43 am 
Newbie

Joined: Mon Jul 23, 2007 5:28 am
Posts: 5
Hibernate version: v3

Name and version of the database you are using: PostgreSQL v8.2

Hi,

I'm using JPA and Hibernate. I have 2 applications.

The first application generate some data, and store into the PostgreSQL database. It works fine. And if i do some requests in the DB, it works fine too.

Now, i have a second application. This one, do some requests on the object that have been store in the DB by the first application.

The problem is, when i do some request on objects that they have been store by another application, it don't work. I have some exception.

I don't understand why. Someone could explain me how to make this work ?

My persistence.xml file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
   <persistence-unit name="Stockholm" transaction-type="RESOURCE_LOCAL">
      <!--  provider -->
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <properties>
         <!-- Classes persistantes -->
         <property name="hibernate.archive.autodetection" value="class, hbm" />
<!--          logs SQL-->
<!--            <property name="hibernate.show_sql" value="true"/>-->
<!--            <property name="hibernate.format_sql" value="true"/>-->
<!--            <property name="use_sql_comments" value="true"/>-->
<!--         -->
         <!-- connexion JDBC -->
         <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
         <property name="hibernate.connection.url" value="jdbc:postgresql:Stockholm" />
         <property name="hibernate.connection.username" value="login" />
         <property name="hibernate.connection.password" value="pwd" />
         <!--  création automatique du schéma -->
         <property name="hibernate.hbm2ddl.auto" value="update" />
         <!-- Dialecte -->
         <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
            <!--  propriétés DataSource c3p0 -->
         <property name="hibernate.c3p0.min_size" value="5" />
         <property name="hibernate.c3p0.max_size" value="20" />
         <property name="hibernate.c3p0.timeout" value="300" />
         <property name="hibernate.c3p0.max_statements" value="50" />
         <property name="hibernate.c3p0.idle_test_period" value="3000" />
      </properties>
   </persistence-unit>
</persistence>


My entityManagerCreation:
Code:
private static EntityManagerFactory emf = Persistence
         .createEntityManagerFactory("Stockholm");


And my entities objects:
Code:
@SuppressWarnings("unused")
@Entity
@Table(name="BIMOBJ")
public class BimObj {
   
   @Id
   @Column(name = "BIM_ID", nullable = false)
   private Integer bim_id;
   
   @Column(name = "VERSION", nullable = false)
   @Version
   private int version;
   
   @Column(name = "ref", length = 30, nullable = true, unique = true)
   private String ref = null;
   
   public BimObj(){
      
   }
   
   public BimObj(int id, String ref){
      this.bim_id = id;
      this.ref = ref;
   }

....
}

@SuppressWarnings("unused")
@Entity
@Table(name="BimData")
public class BimData {
   
   @Id
   @Column(name = "BIMDATA_ID", nullable = false)
   @GeneratedValue(strategy = GenerationType.AUTO)
   private Integer bimData_id;
   
   @Column(name="TIME", nullable = false)
   @Temporal(TemporalType.TIMESTAMP)
   private Date horodate = null;
      
   @OneToOne(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
   @JoinColumn(name = "BIM_ID", unique = false, nullable = false)
   private BimObj bimObj;
   
   public BimData(BimObj b, Date t{
      this.bimObj = b;
      this.horodate = t;
   }

....
}


And here is my exception when i'm trying to work with the object store in the db my the other appli:
Code:
Exception in thread "AWT-EventQueue-0" javax.persistence.PersistenceException: org.hibernate.InstantiationException: No default constructor for entity: common.BimObj
   at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:647)
   at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:73)
   at modele.AccessDB.getAllBimObj(AccessDB.java:326)
   at modele.Gestion.getAllBimObj(Gestion.java:72)
   at vue.SSYClientMainFrame.actionPerformed(SSYClientMainFrame.java:255)
   at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
   at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
   at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
   at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
   at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
   at java.awt.Component.processMouseEvent(Component.java:6038)
   at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
   at java.awt.Component.processEvent(Component.java:5803)
   at java.awt.Container.processEvent(Container.java:2058)
   at java.awt.Component.dispatchEventImpl(Component.java:4410)
   at java.awt.Container.dispatchEventImpl(Container.java:2116)
   at java.awt.Component.dispatchEvent(Component.java:4240)
   at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
   at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
   at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
   at java.awt.Container.dispatchEventImpl(Container.java:2102)
   at java.awt.Window.dispatchEventImpl(Window.java:2429)
   at java.awt.Component.dispatchEvent(Component.java:4240)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Caused by: org.hibernate.InstantiationException: No default constructor for entity: common.BimObj
   at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:84)
   at org.hibernate.tuple.PojoInstantiator.instantiate(PojoInstantiator.java:100)
   at org.hibernate.tuple.entity.AbstractEntityTuplizer.instantiate(AbstractEntityTuplizer.java:351)
   at org.hibernate.persister.entity.AbstractEntityPersister.instantiate(AbstractEntityPersister.java:3539)
   at org.hibernate.impl.SessionImpl.instantiate(SessionImpl.java:1275)
   at org.hibernate.impl.SessionImpl.instantiate(SessionImpl.java:1264)
   at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1290)
   at org.hibernate.loader.Loader.getRow(Loader.java:1197)
   at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:568)
   at org.hibernate.loader.Loader.doQuery(Loader.java:689)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
   at org.hibernate.loader.Loader.doList(Loader.java:2144)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2028)
   at org.hibernate.loader.Loader.list(Loader.java:2023)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:393)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
   at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
   at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:64)
   ... 28 more


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 23, 2007 6:13 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

I see that you have a default constructor for BimObj, but do you have a default contructor for BimData ?

Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 23, 2007 6:36 am 
Newbie

Joined: Mon Jul 23, 2007 5:28 am
Posts: 5
Arf, i've just added the default constructor for BimData, and it works fine ... sometimes !

When i do a request on BimObj:
Code:
List<BimObj> liste = em.createQuery(
            "select b from " + TABLE_BIMOBJ + " as b").getResultList();


I have this exception:
Code:
Exception in thread "AWT-EventQueue-0" javax.persistence.PersistenceException: org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: common.BimData
   at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:647)
   at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:73)
   at modele.AccessDB.getAllAlerte(AccessDB.java:336)
   at modele.Gestion.getAllAlerte(Gestion.java:43)
   at vue.SSYClientMainFrame.actionPerformed(SSYClientMainFrame.java:271)
   at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
   at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
   at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
   at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
   at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
   at java.awt.Component.processMouseEvent(Component.java:6038)
   at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
   at java.awt.Component.processEvent(Component.java:5803)
   at java.awt.Container.processEvent(Container.java:2058)
   at java.awt.Component.dispatchEventImpl(Component.java:4410)
   at java.awt.Container.dispatchEventImpl(Container.java:2116)
   at java.awt.Component.dispatchEvent(Component.java:4240)
   at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
   at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
   at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
   at java.awt.Container.dispatchEventImpl(Container.java:2102)
   at java.awt.Window.dispatchEventImpl(Window.java:2429)
   at java.awt.Component.dispatchEvent(Component.java:4240)
   at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
   at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
   at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
   at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
   at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
   at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
Caused by: org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: common.BimData
   at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:69)
   at org.hibernate.loader.entity.EntityLoader.loadByUniqueKey(EntityLoader.java:85)
   at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:1581)
   at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:365)
   at org.hibernate.type.EntityType.resolve(EntityType.java:306)
   at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:116)
   at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)
   at org.hibernate.loader.Loader.doQuery(Loader.java:717)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
   at org.hibernate.loader.Loader.doList(Loader.java:2144)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2028)
   at org.hibernate.loader.Loader.list(Loader.java:2023)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:393)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
   at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
   at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:64)
   ... 28 more


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 23, 2007 8:03 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

Can you verify what is in the database manually, as i do not think Postgres will allow 2 rows with the same primary key. Can you confirm that a sequence called hibernate_sequence is created in the DB on deployment of your app.

Other than that the only thing i can think of is the c3p0 connection pool settings, but i can see how they could cause this problem.

Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 23, 2007 8:19 am 
Newbie

Joined: Mon Jul 23, 2007 5:28 am
Posts: 5
I've just checked, and there is a hibernate_sequence in my db.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 23, 2007 8:41 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Ok, maybe your model could be wrong.

Does each BimData class ALWAYS contain a diferent BinObj class ? If not this could be the cause of the problem, and you should switch to a @ManyToOne/@OneToMany relationship

Andy

_________________
Rules are only there to be broken


Last edited by andydale on Tue Jul 24, 2007 5:09 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 23, 2007 8:44 am 
Newbie

Joined: Mon Jul 23, 2007 5:28 am
Posts: 5
huh .. no.

A BimData class can contain a same BimObj class.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 23, 2007 9:39 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
I think you should definitely consider trying to map it to a @ManyToOne/@OneToMany as i don't think it is a true OneToOne if a 2 BimData objects can refernece the same BinObj object hence the error you are getting.

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 23, 2007 9:54 am 
Newbie

Joined: Mon Jul 23, 2007 5:28 am
Posts: 5
Thanks for your help, it works !


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

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.