-->
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.  [ 12 posts ] 
Author Message
 Post subject: Strange problem! Plese help!
PostPosted: Mon Feb 09, 2004 8:40 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
I'm using Hibernate 2.1.2 (with 2.1.1 have the same problem).
What I don't understand is why can I insert the first time a ManPrev and right after a HistoricoManPrev and when I try to insert a second ManPrev (which works well) I get the following error when I insert the second HistoricoManPrev:

Code:
net.sf.hibernate.JDBCException: could not insert: [pt.comseal.arsol.vo.HistoricoManPrev#2]
   at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:479)
   at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:443)
   at net.sf.hibernate.impl.ScheduledInsertion.execute(ScheduledInsertion.java:29)
( ... )

Caused by: java.sql.SQLException: ERROR:  fk_historico_man_prev referential integrity violation - key referenced from historico_man_prev not found in mp_mpo

   at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:131)
   at org.postgresql.jdbc1.AbstractJdbc1Connection.ExecSQL(AbstractJdbc1Connection.java:505)
( ... )


And I have the register he's looking for!

In my DB I have in MpMpo:
Code:
(register #1)
man_prev_fk: 1
man_prev_oper_fk: 1
estado: "A"

(register #2)
man_prev_fk: 2
man_prev_oper_fk: 1
estado: "I"


I have the following structure DB:

Code:
historico_man_prev
|
|
|---- > mp_mpo ----------> man_prev_oper
          |
          |
          |-------------> man_prev


historico_man_prev (PK):
   id

mp_mpo (PK):
   man_prev_fk
   man_prev_oper_fk

man_prev (PK):
   id

man_prev_oper (PK):
   id


--------------------------------------------
Mapping files (generated with R3)
--------------------------------------------

HistoricoManPrev
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
   
<hibernate-mapping>
<class
    name="vo.HistoricoManPrev"
    table="historico_man_prev"
>

    <id
        name="id"
        type="long"
        column="id"
    >
        <generator class="increment" />
    </id>

    <property
        name="tipoManPrev"
        type="java.lang.String"
        column="tipo_man_prev"
        length="1"
    />

    <!-- associations -->
    <!-- bi-directional many-to-one association to MpMpo -->
    <many-to-one
        name="mpMpo"
        class="vo.MpMpo"
        not-null="true"
    >
        <column name="man_prev_fk" />
        <column name="man_prev_oper_fk" />
    </many-to-one>

</class>
</hibernate-mapping>


MpMpo
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
   
<hibernate-mapping>
<class
    name="vo.MpMpo"
    table="mp_mpo"
>

    <composite-id name="comp_id" class="vo.MpMpoPK">
        <!-- bi-directional many-to-one association to ManPrevOper -->
        <key-many-to-one
           name="manPrevOper"
           class="vo.ManPrevOper"
       >
           <column name="man_prev_oper_fk" />
       </key-many-to-one>
        <!-- bi-directional many-to-one association to ManPrev -->
        <key-many-to-one
           name="manPrev"
           class="vo.ManPrev"
       >
           <column name="man_prev_fk" />
       </key-many-to-one>
    </composite-id>   

    <property
        name="estado"
        type="java.lang.String"
        column="estado"
        length="1"
    />

    <!-- associations -->
    <!-- bi-directional one-to-many association to HistoricoManPrev -->
    <set
        name="historicoManPrevs"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="man_prev_fk" />
            <column name="man_prev_oper_fk" />
        </key>
        <one-to-many
            class="vo.HistoricoManPrev"
        />
    </set>

</class>
</hibernate-mapping>


ManPrev
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
   
<hibernate-mapping>
<class
    name="vo.ManPrev"
    table="man_prev"
>

    <id
        name="id"
        type="long"
        column="id"
    >
        <generator class="increment" />
    </id>

    <property
        name="tipoManPrev"
        type="java.lang.String"
        column="tipo_man_prev"
        length="1"
    />

    <!-- associations -->
    <!-- bi-directional one-to-many association to MpMpo -->
    <set
        name="mpMpos"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="man_prev_fk" />
        </key>
        <one-to-many
            class="vo.MpMpo"
        />
    </set>

</class>
</hibernate-mapping>


ManPrevOper
Code:
Similar with ManPrev.


---------------------
Testing code
---------------------
To insert a ManPrev:
Code:
ManPrev man_prev = new ManPrev();
man_prev.setTipoManPrev("xpto");
      
MpMpo mp_mpo = new MpMpo();
MpMpoPK mp_mpo_pk = new MpMpoPK();
mp_mpo_pk.setManPrev(man_prev);
      
ManPrevOper man_prev_oper = new ManPrevOper();
man_prev_oper.setId(new Long(1));
mp_mpo_pk.setManPrevOper(man_prev_oper);
mp_mpo.setComp_id(mp_mpo_pk);
      
mp_mpo.setEstado("A");
      
HashSet mp_mpos = new HashSet();
mp_mpos.add(mp_mpo);
      
man_prev.setMpMpos(mp_mpos);

ManPrevBO.inserirManPrev(man_prev);



To insert a HistoricoManPrev:
Code:
HistoricoManPrev historico_man_prev = new HistoricoManPrev();
   
ManPrev man_prev = new ManPrev();
man_prev.setId(new Long(2));
      
MpMpo mp_mpo = new MpMpo();
MpMpoPK mp_mpo_pk = new MpMpoPK();
mp_mpo_pk.setManPrev(man_prev);

ManPrevOper man_prev_oper = new ManPrevOper();
man_prev_oper.setId(new Long(1));
mp_mpo_pk.setManPrevOper(man_prev_oper);

mp_mpo.setComp_id(mp_mpo_pk);
historico_man_prev.setMpMpo(mp_mpo);
      
historico_man_prev.setTipoManPrev("xpto");
      
ManPrevBO.insertHistoricoManPrev(historico_man_prev);


-------------------------
Business method
-------------------------
To insert a ManPrev:
Code:
( ... )
SessionFactory sessionFactory = HibernateFactory.createFactory();   
session = sessionFactory.openSession();
transaction = session.beginTransaction();
         
session.save(man_prev);
         
if(man_prev.getMpMpos() != null) {
   Iterator it = man_prev.getMpMpos().iterator();      
   while(it.hasNext()) { session.save((MpMpo)it.next()); }
}
         
transaction.commit();
( ... )


To insert a HistoricoManPrev:
Code:
( ... )
SessionFactory sessionFactory = HibernateFactory.createFactory();   
session = sessionFactory.openSession();
transaction = session.beginTransaction();
         
session.save(historico_man_prev);
         
transaction.commit();
( ... )



Why isn't he inserting the secong HistoricoManPrev!?
Please help. It's getting me crazy.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 8:50 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Do you reset :
- your db
- Hibernate session
between the 2 tests ?

Do you use the same id for the 2 inserts ?

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 10:02 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
emmanuel wrote:
Do you reset :
- your db
- Hibernate session
between the 2 tests ?

Do you use the same id for the 2 inserts ?


I don't reset my DB.
Each method opens a session and closes the session.
I inserted the first HistoricoManPrev changing only one line in the testing code to man_prev.setId(new Long(1));. It inserted with success. Then changed the line to man_prev.setId(new Long(2)); to insert a HistoricoManPrev related to the second ManPrev and it gave me the error above.


Any idea please?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 11:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
How about you narrowing this a bit down, so we don't have to wade through 5 pages of stuff?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 11:51 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
gloeglm wrote:
How about you narrowing this a bit down, so we don't have to wade through 5 pages of stuff?


In resume, I have HistoricoManPrev which has a FK that points to MpMpo. MpMpo has a composite id of two columns: "man_prev_fk" which points to ManPrev and "man_prev_oper_fk" which points to ManPrevOper.
What I'm trying to do is insert a ManPrev and then insert a HistoricoManPrev (each of them in seperate methods).

Problem (the iterations are made using the testing code above):

Iteration #1:

I insert a ManPrev executing the method for it and then I insert a HistoricoManPrev executing it's method too. It goes great.

Iteration #2:

I insert a ManPrev executing the method for it and it goes great. When I try to insert the second HistoricoManPrev it gives the error above.

My DB has the information it needs (as explain above).
I think the information is all in the post to understand the problem.
If something's missing please tell me.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 11:53 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
method and discipline :)
Try to simplify your mapping to find the smalest that reproduce your issue, then write the most simple method (in a main for example) that reproduce the issue in a synthetic way. It will help you and us a lot.
Plus enable debug mode to check how Hibernate deals with that.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 11:56 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Plus check the ManPrev you add to HistoricoManPrev, it may not be the last inserted/saved one.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 12:30 pm 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
emmanuel wrote:
method and discipline :)
Try to simplify your mapping to find the smalest that reproduce your issue, then write the most simple method (in a main for example) that reproduce the issue in a synthetic way. It will help you and us a lot.
Plus enable debug mode to check how Hibernate deals with that.


Ok. Here's a test class:

Code:
public class TesteAux {

   public static void main(String[] args) {
      
      //---- INSERT A MAN_PREV
      ManPrev man_prev = new ManPrev();
      man_prev.setPrevManutencao("hoje111");
      man_prev.setTipoManPrev("xpto");
      
      MpMpo mp_mpo = new MpMpo();
      MpMpoPK mp_mpo_pk = new MpMpoPK();
      mp_mpo_pk.setManPrev(man_prev);
      
      ManPrevOper man_prev_oper = new ManPrevOper();
      man_prev_oper.setId(new Long(1));
      mp_mpo_pk.setManPrevOper(man_prev_oper);
      
                mp_mpo.setComp_id(mp_mpo_pk);
      
      HashSet mp_mpos = new HashSet();
      mp_mpos.add(mp_mpo);
      
      man_prev.setMpMpos(mp_mpos);

      try {
         ManPrevBO.inserirManPrev(man_prev);
      }
      catch(ManPrevBOException e) {
         System.out.println(e.getMessage());   
      }
      catch(BadFactoryException e) {
         System.out.println(e.getMessage());   
      }
      
      //---- INSERT A HISTORICO_MAN_PREV
      HistoricoManPrev historico_man_prev = new HistoricoManPrev();
   
      man_prev = new ManPrev();
      man_prev.setId(new Long(1)); // ATENTION: switching line!!
      
      mp_mpo = new MpMpo();
      mp_mpo_pk = new MpMpoPK();
      mp_mpo_pk.setManPrev(man_prev);
      man_prev_oper = new ManPrevOper();
      man_prev_oper.setId(new Long(1));
      mp_mpo_pk.setManPrevOper(man_prev_oper);
      mp_mpo.setComp_id(mp_mpo_pk);
      historico_man_prev.setMpMpo(mp_mpo);
      
      historico_man_prev.setTipoManPrev("xpto");
      
      try {
         ManPrevBO.inserirHistoricoManPrev(historico_man_prev);
      }
      catch(ManPrevBOException e) {
         System.out.println(e.getMessage());   
      }
      catch(BadFactoryException e) {
         System.out.println(e.getMessage());   
      }
   }


When I execute this class for the first time, it goes great. For the second execution I pass id "2" to man_prev.setId(new Long(2)).
The ManPrev is inserted but the HistoricoManPrev gives an erro of violation of integrity.

------------------------
DEBUG INFO
------------------------
Code:
( ... )
DEBUG: fetching initial value: select max(id) from historico_man_prev
DEBUG: first free id: 2
DEBUG: saving [vo.HistoricoManPrev#2]
DEBUG: id unsaved-value strategy NONE
DEBUG: id unsaved-value strategy NULL
DEBUG: commit
DEBUG: flushing session
DEBUG: Flushing entities and processing referenced collections
DEBUG: Processing unreferenced collections
DEBUG: Scheduling collection removes/(re)creates/updates
DEBUG: Flushed: 1 insertions, 0 updates, 0 deletions to 1 objects
DEBUG: Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
DEBUG: listing entities:
DEBUG: vo.HistoricoManPrev{mpMpo=MpMpo#MpMpoPK{manPrevOper=ManPrevOper#1, manPrev=ManPrev#2}, tipoManPrev=E, id=2}
DEBUG: executing flush
DEBUG: Inserting entity: [vo.HistoricoManPrev#2]
DEBUG: about to open: 0 open PreparedStatements, 0 open ResultSets
DEBUG: insert into historico_man_prev (tipo_man_prev, man_prev_fk, man_prev_oper_fk, id) values (?, ?, ?, ?)
DEBUG: preparing statement
DEBUG: Dehydrating entity: [vo.HistoricoManPrev#2]
DEBUG: binding 'E' to parameter: 1
DEBUG: id unsaved-value strategy NONE
DEBUG: id unsaved-value strategy NULL
DEBUG: binding '1' to parameter: 2
DEBUG: id unsaved-value strategy NULL
DEBUG: binding '2' to parameter: 3
DEBUG: id unsaved-value strategy NULL

DEBUG: binding '2' to parameter: 4
DEBUG: done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG: closing statement
DEBUG: SQL Exception
java.sql.SQLException: ERROR:  fk_historico_man_prev referential integrity violation - key referenced from historico_man_prev not found in mp_mpo
( ... )


By looking at the debug information I'm seeing a strange thing. The binding for parameters "man_prev_fk" and "man_prev_oper_fk" are switch!!

It should be:

Code:
DEBUG: binding '2' to parameter: 2
DEBUG: id unsaved-value strategy NULL
DEBUG: binding '1' to parameter: 3
DEBUG: id unsaved-value strategy NULL


and not:

Code:
DEBUG: binding '1' to parameter: 2
DEBUG: id unsaved-value strategy NULL
DEBUG: binding '2' to parameter: 3
DEBUG: id unsaved-value strategy NULL


That's why he can't see the second ManPrev that I've inserted. He's looking in the wrong table.
Is this a possible bug?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 12:45 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Code:
<column name="man_prev_fk" />
<column name="man_prev_oper_fk" />

Should be
Code:
<column name="man_prev_oper_fk" />
<column name="man_prev_fk" />


FK declaration must be in the same order than in the composite-id. Otherwise Hibernate can't properly map. Check every association using composite id.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 1:00 pm 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
emmanuel wrote:
Code:
<column name="man_prev_fk" />
<column name="man_prev_oper_fk" />

Should be
Code:
<column name="man_prev_oper_fk" />
<column name="man_prev_fk" />


FK declaration must be in the same order than in the composite-id. Otherwise Hibernate can't properly map. Check every association using composite id.


Your right. It worked.
But then this is a bug of Middlegen. I generated the mapping files with R3.
This should be reported or viewed, right?

Thanks VERY much. This kind of problem had 2 months.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 1:06 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Not that easy to find from outside becauseof you complex domain model using composite-id.

Yes, post a new simple thread and wait for David answer then add it to the Middlegen JIRA.

This works afterall, cool :)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 09, 2004 1:21 pm 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
emmanuel wrote:
Not that easy to find from outside becauseof you complex domain model using composite-id.

Yes, post a new simple thread and wait for David answer then add it to the Middlegen JIRA.

This works afterall, cool :)


Yep. :-)
And thanks to you, my mind is now very light.
Thanks again.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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.