-->
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: auto generate a property field
PostPosted: Wed Jan 25, 2006 9:25 am 
Newbie

Joined: Mon Jan 23, 2006 8:26 am
Posts: 6
how can i make a mapping property (lpos) to become incremental by not using id because i already have an id that is assigned but i need another property to be incremental. making lpos an id will throw an duplicate id constraint error. is there a way to put a generator for a property mapping?

this existing can save my data up to the 2nd level. but i cannot load it properly because lpos is always zero (0), but when i edit the records directly can modify the lpos 0..N respectively, i can load the record properly. please help. thx...


Hibernate version:
3.1

Mapping documents:
Code:
<class name="billing.collections.GeneralCollections" table="gc">
   <id name="id"><generator class="assigned"/></id>   
   <property name="payor" type="string"/>
   <property name="tenantId" type="string"/>
   <property name="leaseId" type="string"/>
   <property name="trade" type="string"/>
   <property name="business" type="string"/>
   <property name="lname" type="string"/>
   <property name="fname" type="string"/>
   <property name="mi" type="string"/>
   <property name="date" type="string"/>
   <property name="invoice" type="string"/>
   <property name="particulars" type="string"/>
   <property name="periodStart" type="string"/>
   <property name="periodEnd" type="string"/>   
   <list name="collections" table="gc_col" lazy="true"
      inverse="true" cascade="all">      
      <key column="gcId"/>
      <index column="lpos" />
      <one-to-many class="billing.collections.GCCollection" />
   </list>
   <list name="orVat" table="ors" lazy="true"
      inverse="true" cascade="all" where="type=1">
      <key column="ref"/>
      <index column="lpos" />
      <one-to-many class="com.payment.OR" />
   </list>
   <list name="orNonVat" table="ors" lazy="true"
      inverse="true" cascade="all" where="type=0">
      <key column="ref"/>
      <index column="lpos" />
      <one-to-many class="com.payment.OR" />
   </list>
</class>

<class name="billing.collections.GCCollection" table="gc_col">
   <id name="id"><generator class="assigned"/></id>
   <property name="lpos"/>
   <property name="gcId" type="string"/>
   <property name="defId" type="string"/>     
   <property name="desc" column="description" type="string"/>   
   <property name="payment" type="string"/>
   <property name="type" type="string"/>
   <property name="vat"/>
   <property name="entryAmount" type="string"/>
   <property name="cashAmount" type="string"/>
   <property name="checkAmount" type="string"/>
</class>

<class name="com.payment.OR" table="ors">
   <id name="id" type="string"><generator class="assigned"/></id>
   <property name="ref" type="string"/>
   <property name="type" type="string"/>
   <property name="orNumber" type="string"/>
   <property name="status" type="string"/>      
   <list name="entries" table="or_entry" lazy="true"
      inverse="true" cascade="all">
      <key column="orId"/>
      <index column="lpos" />
      <one-to-many class="com.payment.OREntry" />
   </list>
</class>

<class name="com.payment.OREntry" table="or_entry">
   <id name="id" type="string"><generator class="assigned"/></id>      
   <property name="orId" type="string"/>
   <property name="ref" type="string"/>
   <property name="amount" type="string"/>
   <!--<property name="isDeduction"/>-->
   <property name="endPeriod" type="string"/>
   <property name="itemId" type="string"/>
</class>


Code between sessionFactory.openSession() and session.close():
Code:
n/a


Full stack trace of any exception that occurs:
Code:
n/a


Name and version of the database you are using:
Code:
Mysql 5.0.1


The generated SQL (show_sql=true):


Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 11:10 am 
Beginner
Beginner

Joined: Thu Oct 20, 2005 1:03 pm
Posts: 38
i believe that you are misusing the id tag. the id tag can be any field in the database - if it's lpos, then the column value should be lpos for the id tag and the attribute generated - like this and the id property is simply a property:

<class name="billing.collections.GeneralCollections" table="gc">
<id name="lpos"><generator class="generated"/></id>
<property name="id" column="ID"/>

. . .


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 3:17 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
the same column cannot be used for multiple list-index values...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 9:54 pm 
Newbie

Joined: Mon Jan 23, 2006 8:26 am
Posts: 6
i need the column id to be the id because the one-to-many maps to the defined id, right? the id is what i link to the other table, lpos however, must be a integer from 0...N,

example:
GeneralCollection has 5 GCCollection object, lpos of the records should 0,1,2,3,4. i did a test and directly edit the lpos and put 10, there will be 6 extra objects that is created.

just a question, what is the right way to do list mapping?

any help or example will be greatly appreciated. thx...


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 10:29 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
For indexed lists like you have, the index is accessible from the containing class. You've already got this: the value of the lpos column is accessible as
Code:
genCol.getCollections().indexOf(collection);
To make this lpos column available (read-only) from inside the Collection list, the easiest thing to do is to map lpos as a formula:
Code:
<property name="lpos" formula="lpos"/>
This will allow hibernate to maintain lpos (as the index of the list) and also allows the user to find out a list element's index without having the list.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 4:56 am 
Newbie

Joined: Mon Jan 23, 2006 8:26 am
Posts: 6
thanks for the help... its working now


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 4:24 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Excellent. Who gets the credit?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 4:53 pm 
Newbie

Joined: Tue Jun 20, 2006 11:51 am
Posts: 14
tenwit wrote:
For indexed lists like you have, the index is accessible from the containing class. You've already got this: the value of the lpos column is accessible as
Code:
genCol.getCollections().indexOf(collection);
To make this lpos column available (read-only) from inside the Collection list, the easiest thing to do is to map lpos as a formula:
Code:
<property name="lpos" formula="lpos"/>
This will allow hibernate to maintain lpos (as the index of the list) and also allows the user to find out a list element's index without having the list.


Tenwit, could you elaborate on this strategy? (possibly with a simple example including mappings & code)

I'm not sure where the following goes or what the objects are in relation to cyber's example:
Code:
genCol.getCollections().indexOf(collection);


I'm also not clear as to whether the parent should be marked inverse or whether there should be some other type of mapping.


Thanks,
Gary


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 6:18 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Code:
<class name="ContainingClass" ...>
  ...
  <list name="Things" ...>
    <key column="keyCol"/>
    <list-index column="indexCol"/>
    <one-to-many class="Thing"/>
  ...
</class>
<class name="Thing" ...>
  ...
  <property name="MyIndexInContainingClassList" formula="indexCol"/>
  ...
</class>

Your java code could now safely include code like
Code:
for (int i = 0; i < containingObject.getThings().size(); ++i)
{
  Thing t = containingObject.getThings().get(i);
  if (t.getMyIndexInContainingClassList() != i)
  {
    App.getDeveloper().setSalary(0);
  }
}
And you'd never have to worry about working for free :)

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 6:52 pm 
Newbie

Joined: Tue Jun 20, 2006 11:51 am
Posts: 14
Thanks for the quick reply. I'm still not getting it to work (list-index column is null). Can you take a look at the following and see what I'm doing wrong?

Thx,
Gary

parent mapping
Code:
<hibernate-mapping>
   <class name="mil.jfcom.model.Survey" table="SURVEY">
      <id name="id" column="id" unsaved-value="null">
         <generator class="increment" />
      </id>
      <property name="title" length="50" />
      <property name="description" length="150" />
      <list name="pages" table="PAGE" lazy="true" inverse="true" cascade="all-delete-orphan">
         <key column="SURVEY_ID" />
         <list-index column="PAGE_NUM" base="1" />
         <one-to-many class="mil.jfcom.model.Page" />
      </list>
   </class>
</hibernate-mapping>


child mapping
Code:
<hibernate-mapping>
   <class name="mil.jfcom.model.Page" table="PAGE">
      <id name="id" column="id" unsaved-value="null">
         <generator class="increment" />
      </id>
      <property name="pageNumber" formula="PAGE_NUM" />
      <property name="title" length="50" />
      <many-to-one name="survey" column="SURVEY_ID" not-null="true" />
   </class>
</hibernate-mapping>


parent code
Code:
public class Survey {
   
   private Long id;
   private String title;
   private String description;
   private List pages = new ArrayList();
   
   public Survey() {
   }
   
   public Long getId() {
      return id;
   }
   public void setId(Long id) {
      this.id = id;
   }
   
   public String getTitle() {
      return title;
   }
   public void setTitle(String title) {
      this.title = title;
   }
   
   public String getDescription() {
      return description;
   }
   public void setDescription(String description) {
      this.description = description;
   }

   public List getPages() {
      return pages;
   }
   
   public void setPages(List pages) {
      this.pages = pages;
   }
   
   public void addPage(Page page) {
      page.setSurvey(this);
      pages.add(page);
   }
}


child code
Code:
public class Page {
   
   private Long id;
   private Integer pageNumber;
   private String title;
   private Survey survey;
   
   public Page() {
   }
   
   public Page(String title) {
      this.title = title;
   }
   
   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public Integer getPageNumber() {
      return pageNumber;
   }
   public void setPageNumber(Integer pageNumber) {
      this.pageNumber = pageNumber;
   }
   
   public String getTitle() {
      return title;
   }
   public void setTitle(String title) {
      this.title = title;
   }

   public Survey getSurvey() {
      return survey;
   }
   public void setSurvey(Survey survey) {
      this.survey = survey;
   }
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 7:14 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You have inverse="true". You have to choose between one of these two scenarios:
  1. Pages exist only within lists belonging to Surveys, in which case you must remove inverse="true" and any code that saves Pages outside of a survey
  2. Pages may exist outside of surveys, in which case you must allow PAGE_NUM to be null: pages with null page_num are not in a survey.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 21, 2006 7:26 pm 
Newbie

Joined: Tue Jun 20, 2006 11:51 am
Posts: 14
That did it. Your explanation made the underlying reason seem so obviously simple.

I tried unsuccessfully to give your answer credit. I guess you can't do that if you didn't start the thread.

Thanks again,
Gary


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.