-->
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.  [ 26 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: update performs update and delete
PostPosted: Thu May 27, 2004 11:08 am 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
Hibernate version: 2.1.2

Creation of a new user works perfect! Changing the company of a user works perfectly. Adding the user to a group works perfectly!

But when I want to update a user
Hibernate updates the user - Hibernate: Update USERACCOUNT set firstn..
Hibernate deletes the user from the group??? - Hibernate: Delete from USERGROUP where userId=?
Hibernate updates the document authorID (userID)

WHAT AM I MISSING???? :S


MAPPING DOCUMENTS

USERACCOUNT.HBM.XML

Code:
<hibernate-mapping>
<class name="UserAccount" table="USERACCOUNT">
  <id column="userID" name="id" type="integer" unsaved-value="0">
   <generator class="increment"/>
  </id>
  <property name="firstname" type="string"/>
  <property name="lastname" type="string"/>
  <property name="address" type="string"/>
  <property name="zipcode" type="string"/>
  <property name="city" type="string"/>
  <property name="telephone" type="string"/>
  <property name="email" type="string"/>
  <property name="username" type="string"/>
  <property name="password" type="string"/>
  <property name="old_password" type="string"/>
  <property name="last_login" type="date"/>
  <property name="status" type="string"/>
 
  <many-to-one column="companyID" name="company"/>

  <set lazy="false" name="groups" table="USERGROEP">
   <key column="userID"/>
   <many-to-many class="Group" column="groepID"/>
  </set>

  <set lazy="false" name="documents" table="DOCUMENT">
   <key column="authorID"/>
   <one-to-many class="Document"/>
  </set>
 
</class>
</hibernate-mapping>


DOCUMENT.HBM.XML

Code:
<hibernate-mapping>
<class name="Group" table="GROUP">
  <id column="groupID" name="id" type="int" unsaved-value="0">
   <generator class="increment"/>
  </id>
  <property name="name" type="string"/>
  <property name="description" type="string"/>
  <bag inverse="true" lazy="false" name="users" table="USERGROUP">
   <key column="groupID"/>
   <many-to-many class="UserAccount" column="userID"/>
  </bag>
</class>
</hibernate-mapping>


Java code that performs the update of the user
Code:
session = HibernateDAOFactory.currentSession();
transaction = session.beginTransaction();
Company company = transferUser.getCompany();
if (company != null)
{
   company.getUsers().add(transferUser);
}
         
session.saveOrUpdate(transferUser);
transaction.commit();
HibernateDAOFactory.closeSession();


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 3:11 am 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
Am I the only one having this problem??


Top
 Profile  
 
 Post subject: Re: update performs update and delete
PostPosted: Fri May 28, 2004 3:17 am 
Regular
Regular

Joined: Wed May 12, 2004 3:03 am
Posts: 51
Location: France
-FoX- wrote:
<set lazy="false" name="groups" table="USERGROEP">
<key column="userID"/>
<many-to-many class="Group" column="groepID"/>
</set>
[/code]


Is this a typing mistake?


Top
 Profile  
 
 Post subject: Re: update performs update and delete
PostPosted: Fri May 28, 2004 3:23 am 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
_charles_ wrote:
-FoX- wrote:
<set lazy="false" name="groups" table="USERGROEP">
<key column="userID"/>
<many-to-many class="Group" column="groepID"/>
</set>
[/code]


Is this a typing mistake?

Oh, yes.. I accidentely changed it, but it is correct in my mapping files!


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 3:41 am 
Regular
Regular

Joined: Wed May 12, 2004 3:03 am
Posts: 51
Location: France
Why don't you update the company too?
The company object changed... you should update it...

Could you please give more details on your associations?

Thanks,

Charles


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 3:47 am 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
_charles_ wrote:
Why don't you update the company too?
The company object changed... you should update it...

Could you please give more details on your associations?

Thanks,

Charles

So, should I just do?:
Code:
session.saveOrUpdate(company);
session.saveOrUpdate(transferUser);



I have the following tables:

USERACCOUNT 1 ----- * USERGROUP * ------ 1 GROUP

COMPANY 1 ----- * USERACCOUNT

I don't understand why hibernate deletes the link in USERGROUP..?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 3:56 am 
Regular
Regular

Joined: Wed May 12, 2004 3:03 am
Posts: 51
Location: France
I must admit that I don't really know why Hibernate delete the group reference.

But, maybe it comes from the origin of the object transferUser.

Could you show the USERGROUP mapping file, and the java code of the origin of the transferUser object?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 4:04 am 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
This is my GROUP mapping file
Code:
<hibernate-mapping>
<class name="Group" table="GROUP">
  <id column="groupID" name="id" type="int" unsaved-value="0">
   <generator class="increment"/>
  </id>
  <property name="name" type="string"/>
  <property name="description" type="string"/>
  <bag inverse="true" lazy="false" name="users" table="USERGROUP">
   <key column="groupID"/>
   <many-to-many class="UserAccount" column="userID"/>
  </bag>
</class>
</hibernate-mapping>


I don't have a usergroup mapping file, but doesn't Hibernate provides the link between useraccount & group?

transferUser is a bean with following content:
Code:
private Integer id;
private String firstname;
private String lastname;
private String address;
private String zipcode;
private String city;
private String telephone;
private String email;
private String username;
private String password;
private String old_password;
private Date last_login;
private String status;
private Company company;
private Set groups;
private Set documents;

// getters & setters..


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 4:33 am 
Regular
Regular

Joined: Wed May 12, 2004 3:03 am
Posts: 51
Location: France
Sorry but I didn't mean the description of transferUser, but where does the instance come from...


Quote:
session = HibernateDAOFactory.currentSession();
transaction = session.beginTransaction();
Company company = transferUser.getCompany();
if (company != null)
{
company.getUsers().add(transferUser);
}

session.saveOrUpdate(transferUser);
transaction.commit();
HibernateDAOFactory.closeSession();


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 4:41 am 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
_charles_ wrote:
Sorry but I didn't mean the description of transferUser, but where does the instance come from...

Oh, I pass this useraccount-bean with the method:

Here you can see my full method:
Code:
public UserAccount insertUserAccount(UserAccount transferUser)
{
   Session session = null;
   Transaction transaction = null;
   
   try
   {
      session = HibernateDAOFactory.currentSession();
      transaction = session.beginTransaction();
      Company company = transferUser.getCompany();
      if (company != null)
      {
         company.getUsers().add(transferUser);
      }
      Set groups = transferUser.getGroups();

      if (groups != null)
      {
         for(Iterator i = groups.iterator(); i.hasNext();)
         {
         Group group = (Group) i.next();
         group.getUsers().add(transferUser);
         }
      }
      else
      {
         log.info("UserAccount: No groups specified");
      }
         
      session.saveOrUpdate(transferUser);
      transaction.commit();
      HibernateDAOFactory.closeSession();
      insertBoolean = true;
   }
   catch(HibernateException he)
   {
      log.error(he);
      if(transaction != null)
      {
           log.debug("Rolling back transaction");
         try
         {
         transaction.rollback();
         insertBoolean = false;
         }
         catch (HibernateException hte)
         {
            log.error("Error on rolling back transaction");
         }
      }
   }
   catch(Exception e)
   {
      log.error("Unhandled exception", e);
   }
      
   return (insertBoolean == true) ? transferUser : null;
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 5:15 am 
Regular
Regular

Joined: Wed May 12, 2004 3:03 am
Posts: 51
Location: France
I think you should update the group when you call
Code:
group.getUsers().add(transferUser);


Maybe this is the problem's reason...

Hope this helps,

Tell me if it's ok

Charles


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 5:46 am 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
_charles_ wrote:
I think you should update the group when you call
Code:
group.getUsers().add(transferUser);


Maybe this is the problem's reason...

Hope this helps,

Tell me if it's ok

Charles


good tip, but it didn't resolve my problem???

Damn, why is hibernate deleting my relation???


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 9:19 am 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
Maybe someone of the developers know what is going on here..?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 9:22 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
I guess you don't handle the collection correctly. Broken get/set pair or something. Use your debugger, it is much quicker than posting in forums.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 28, 2004 10:20 am 
Beginner
Beginner

Joined: Thu May 06, 2004 5:30 am
Posts: 45
Pfff, I'm going mad :(:(:(

Here is the debug-log.. I can't find anything that could point to the problem

Code:
DEBUG - Mapped property: documents, type: java.util.Set
DEBUG - null<-org.dom4j.tree.DefaultAttribute@1cdfd19 [Attribute: name resource value "Group.hbm.xml"]
INFO  - Mapping resource: Group.hbm.xml
DEBUG - trying to locate http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd in classpath under net/sf/hibernate/
DEBUG - found http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd in classpath
INFO  - Mapping class: Group -> GROUP
DEBUG - Mapped property: id -> groupID, type: integer
DEBUG - Mapped property: name -> name, type: string
DEBUG - Mapped property: description -> description, type: string
INFO  - Mapping collection: Group.users -> USERGROUP
DEBUG - Mapped property: users, type: java.util.Collection
DEBUG - null<-org.dom4j.tree.DefaultAttribute@a37c6a [Attribute: name resource value "Company.hbm.xml"]
INFO  - Mapping resource: Company.hbm.xml
DEBUG - trying to locate http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd in classpath under net/sf/hibernate/
DEBUG - found http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd in classpath

INFO  - Mapping class: Company -> COMPANY
DEBUG - Mapped property: id -> companyID, type: integer
DEBUG - Mapped property: name -> name, type: string
DEBUG - Mapped property: address -> address, type: string
DEBUG - Mapped property: zipcode -> zipcode, type: string
DEBUG - Mapped property: city -> city, type: string
DEBUG - Mapped property: telephone -> telephone, type: string
DEBUG - Mapped property: fax -> fax, type: string
DEBUG - Mapped property: email -> email, type: string
DEBUG - Mapped property: vat -> vat, type: string
DEBUG - Mapped property: bankaccount -> bankaccount, type: string
DEBUG - Mapped property: website -> website, type: string
DEBUG - Mapped property: users, type: java.util.Collection
DEBUG - null<-org.dom4j.tree.DefaultAttribute@13d21d6 [Attribute: name resourcevalue "Document.hbm.xml"]
INFO  - Mapping resource: Document.hbm.xml
DEBUG - trying to locate http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd in classpath under net/sf/hibernate/
DEBUG - found http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd in classpath
INFO  - Mapping class: Document -> DOCUMENT
DEBUG - Mapped property: id -> docID, type: integer
DEBUG - Mapped property: title -> title, type: string
DEBUG - Mapped property: description -> description, type: string
DEBUG - Mapped property: keywords -> keywords, type: string
DEBUG - Mapped property: filename -> filename, type: string
DEBUG - Mapped property: filesize -> filesize, type: integer
DEBUG - Mapped property: contenttype -> contenttype, type: string
DEBUG - Mapped property: path -> path, type: string
DEBUG - Mapped property: version -> version, type: string
DEBUG - Mapped property: creation_date -> creation_date, type: date
DEBUG - Mapped property: modified_date -> modified_date, type: date
DEBUG - Mapped property: expire_date -> expire_date, type: date
DEBUG - Mapped property: isLocked -> isLocked, type: boolean
DEBUG - Mapped property: comment -> comment, type: string
DEBUG - Mapped property: user -> authorID, type: UserAccount
INFO  - Configured SessionFactory: null
DEBUG - properties: {PATHS & PROPERTIES -- cut --}
INFO  - processing one-to-many association mappings
DEBUG - Second pass for collection: UserAccount.groups
DEBUG - Mapped collection key: userID, element: groupID, type: Group
DEBUG - Second pass for collection: UserAccount.documents
INFO  - Mapping collection: UserAccount.documents -> DOCUMENT
DEBUG - Mapped collection key: authorID, one-to-many: Document
DEBUG - Second pass for collection: Group.users
DEBUG - Mapped collection key: groupID, element: userID, type: UserAccount
DEBUG - Second pass for collection: Company.users
INFO  - Mapping collection: Company.users -> USERACCOUNT
DEBUG - Mapped collection key: companyID, one-to-many: UserAccount
INFO  - processing one-to-one association property references
INFO  - processing foreign key constraints
DEBUG - resolving reference to class: UserAccount
DEBUG - resolving reference to class: Company
DEBUG - resolving reference to class: Group
DEBUG - resolving reference to class: UserAccount

INFO  - Using dialect: net.sf.hibernate.dialect.MySQLDialect
INFO  - Use outer join fetching: true
INFO  - Using Hibernate built-in connection pool (not for production use!)
INFO  - Hibernate connection pool size: 20
INFO  - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/dms?autoReconnect=true
INFO  - connection properties: {user=root, password=}
INFO  - Transaction strategy: net.sf.hibernate.transaction.JDBCTransactionFactory
INFO  - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
DEBUG - total checked-out connections: 0
DEBUG - opening new JDBC connection
DEBUG - created connection to: jdbc:mysql://localhost:3306/dms?autoReconnect=true, Isolation Level: 4
DEBUG - returning connection to pool, pool size: 1
INFO  - Use scrollable result sets: true
INFO  - Use JDBC3 getGeneratedKeys(): true
INFO  - Optimize cache for minimal puts: false
INFO  - echoing all SQL to stdout
INFO  - Query language substitutions: {}
INFO  - cache provider: net.sf.ehcache.hibernate.Provider
INFO  - instantiating and configuring caches
INFO  - building session factory
DEBUG - instantiating session factory with properties: {--CUT--}
DEBUG - registered: 8a8080d4fccbefb000fccbf00cf50004 (unnamed)
INFO  - no JNDI name configured
DEBUG - instantiated session factory
INFO  - Initialisation of Hibernate is finished
DEBUG - begin
DEBUG - current autocommit status:false
INFO  - UserAccount: No groups specified
DEBUG - id unsaved-value: 0
DEBUG - saveOrUpdate() previously saved instance with id: 2
DEBUG - updating [UserAccount#2]
DEBUG - collection dereferenced while transient [UserAccount.groups#2]
DEBUG - collection dereferenced while transient [UserAccount.documents#2]
DEBUG - commit
DEBUG - flushing session
DEBUG - Flushing entities and processing referenced collections
DEBUG - Updating entity: [UserAccount#2]
DEBUG - Processing unreferenced collections
DEBUG - Scheduling collection removes/(re)creates/updates
DEBUG - Flushed: 0 insertions, 1 updates, 0 deletions to 1 objects
DEBUG - Flushed: 0 (re)creations, 0 updates, 2 removals to 0 collections
DEBUG - listing entities:
DEBUG - UserAccount{password=098f6bcd4621d373cade4e832627b4f6, old_password=, last_login=28 may 2004,
address=Technologroad 11, telephone=9999/99.39.02, status=, id=2, documents=null, company=null, lastname=LastTestName,
groups=null, firstname=FirstTestName, username=testuser, email=testuser@test.com, zipcode=305501, city=NoWhere}
DEBUG - executing flush
DEBUG - Updating entity: [UserAccount#2]
DEBUG - about to open: 0 open PreparedStatements, 0 open ResultSets
DEBUG - update USERACCOUNT set firstname=?, lastname=?, address=?, zipcode=?, city=?, telephone=?, email=?,
username=?, password=?, old_password=?, last_login=?, status=?, companyID=? where userID=?

Hibernate: update USERACCOUNT set firstname=?, lastname=?, address=?, zipcode=?, city=?, telephone=?, email=?,
username=?, password=?, old_password=?, last_login=?, status=?, companyID=? where userID=?

DEBUG - preparing statement
DEBUG - Dehydrating entity: [UserAccount#2]
DEBUG - binding 'TestFirstName' to parameter: 1
DEBUG - binding 'TestLastName' to parameter: 2
DEBUG - binding Technologroad 11' to parameter: 3
DEBUG - binding '305501' to parameter: 4
DEBUG - binding 'NoWhere' to parameter: 5
DEBUG - binding '9999/99.39.02' to parameter: 6
DEBUG - binding 'testuser@test.com' to parameter: 7
DEBUG - binding 'testuser' to parameter: 8
DEBUG - binding '098f6bcd4621d373cade4e832627b4f6' to parameter: 9
DEBUG - binding '' to parameter: 10
DEBUG - binding '28 may 2004' to parameter: 11
DEBUG - binding '' to parameter: 12
DEBUG - binding null to parameter: 13
DEBUG - binding '2' to parameter: 14
DEBUG - Adding to batch
DEBUG - Executing batch size: 1
DEBUG - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG - closing statement
DEBUG - Deleting collection: [UserAccount.groups#2]
DEBUG - about to open: 0 open PreparedStatements, 0 open ResultSets
[b]DEBUG - delete from USERGROUP where userID=?
Hibernate: delete from USERGROUP where userID=?[/b]
DEBUG - preparing statement
DEBUG - binding '2' to parameter: 1
DEBUG - Adding to batch
DEBUG - done deleting collection
DEBUG - Deleting collection: [UserAccount.documents#2]
DEBUG - Executing batch size: 1
DEBUG - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG - closing statement
DEBUG - about to open: 0 open PreparedStatements, 0 open ResultSets
DEBUG - update DOCUMENT set authorID=null where authorID=?
Hibernate: update DOCUMENT set authorID=null where authorID=?
DEBUG - preparing statement
DEBUG - binding '2' to parameter: 1
DEBUG - Adding to batch
DEBUG - done deleting collection
DEBUG - Executing batch size: 1
DEBUG - done closing: 0 open PreparedStatements, 0 open ResultSets
DEBUG - closing statement
DEBUG - post flush
DEBUG - transaction completion
DEBUG - flushing session
DEBUG - Flushing entities and processing referenced collections
DEBUG - Processing unreferenced collections
DEBUG - Scheduling collection removes/(re)creates/updates
DEBUG - Flushed: 0 insertions, 0 updates, 0 deletions to 1 objects
DEBUG - Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
DEBUG - listing entities:
DEBUG - UserAccount{password=098f6bcd4621d373cade4e832627b4f6, old_password=, last_login=28 may 2004,
address=Technologroad 11, telephone=9999/99.39.02, status=, id=2, documents=null, company=null, lastname=LastTestName,
groups=null, firstname=FirstTestName, username=testuser, email=testuser@test.com, zipcode=305501, city=NoWhere}
DEBUG - executing flush
DEBUG - post flush
DEBUG - closing session
DEBUG - disconnecting session
DEBUG - returning connection to pool, pool size: 1
DEBUG - transaction completion


Please help.. I'm getting desperate :( ??


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 26 posts ]  Go to page 1, 2  Next

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.