-->
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.  [ 3 posts ] 
Author Message
 Post subject: List-index prob
PostPosted: Sat Apr 28, 2007 8:14 am 
Newbie

Joined: Fri Apr 27, 2007 5:05 pm
Posts: 7
Hi,
i have got a problem with setting the index of a List in may database-entries.

I have got an User-Object (mapping):


Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping schema="..." package="...">
  <class name="User" table="user" schema="..." optimistic-lock="none">
    <id name="id" type="integer" unsaved-value="null" column="id">
      <generator class="native"/>
    </id>
    <property name="userName" type="string" column="user_name"/>
   
    ...
   
    <list name="characters" lazy="false" cascade="none">
      <key foreign-key="User_id" update="false" column="user_id" />
      <list-index base="0" column="characters_idx"/>
      <one-to-many entity-name="com.g.dto.Character"/>
    </list>
  </class>
</hibernate-mapping>



A Character-Object (mapping):

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping schema="..." package="...">
  <class name="Character" table="character" schema="..." optimistic-lock="none">
    <id name="id" type="integer" unsaved-value="null" column="id">
      <generator class="native"/>
    </id>
   
    ...

    <many-to-one name="user" entity-name="com.g.dto.User" cascade="none" foreign-key="User_id" not-null="true" column="user_id"/>
  </class>
</hibernate-mapping>



I add a new character to the Userlist and stores the character with this snippet:

Code:
public static void addCharacter(Character character){
      
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      
      // gets userobject from Session
      // adds character to User and saves character
      User user = JSFUtil.getManagedUserObject();
      character.setUser(user);
      user.getCharacters().add(character);
      
      session.beginTransaction();
      session.save(character);
      session.getTransaction().commit();
      
      
   }


Everything works fine, but the index of my characterList will be not set. If the character stored in the DataBase characters_idx is NULL. How could I set this index? Or should User make that automaticly? Couldnt I add a Character to the Database with adding that character to Users-Characterlist and session.save(User) instead of save the character into the DB?

I hope anyone can help me
Thnx
Alex


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 28, 2007 8:23 am 
Senior
Senior

Joined: Sat Apr 21, 2007 11:01 pm
Posts: 144
I'm sure Hibernatre doen't do cascade saves and if it does you need to configure it specificaly to do so. In a production system I wouldn't recomened doing that anyway. You should save your char object and then set that object on the user and then save your user.

Hope that helps.

_________________
Everytime you get an answer to your question without giving credit; god kills a kitten. :(


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 28, 2007 10:25 am 
Newbie

Joined: Fri Apr 27, 2007 5:05 pm
Posts: 7
Hi,
thanks I will do save it that way. :)
But have I to use the index-tag if I use a generic List (User.characterList)? And if I have to set the Charakter-index, could I take the size of the User.characterList and save the new character with size+1 as index?

Thnx
Alex

EDIT: I solved my problem with the Hibernate Bag :)


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