-->
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.  [ 11 posts ] 
Author Message
 Post subject: Help on having a primary key being used as a linst-index
PostPosted: Mon May 22, 2006 10:47 am 
Newbie

Joined: Mon May 22, 2006 10:23 am
Posts: 7
Hello,
Im not managing to have a primary key being used as a linst-index.

I have 2 POJOS A,B.
A objects have a List of B objects

<class name="org.test.A" table="A">
<id name="kip"/>
<list name="bs">
<key column="kip" not-null="true" />
<list-index column="id" />
<one-to-many class="org.test.B"/>
</list>
</class>

<class name="org.test.B" table="B">
<id name="id" column="id" type="integer">
<generator class="identity"/>
</id>
<property name="kip" insert="false" update="false"/>
</class>

hibernate tools is creating all tables just as i espected.
The problem is that i am not being able to have the primary key of B as a list-index. (i have tryed giving other name to the index and it works well, but creating a new column :P )
How can i do this?
TIA
marefado

Hibernate version: 3.1


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 11:02 am 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
Are you taking some kind or error or is your list being populated teh wrong way, not beeing populated at all?????


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 11:13 am 
Newbie

Joined: Mon May 22, 2006 10:23 am
Posts: 7
Hello,
Thank you for your help, the error is:

Initial SessionFactory creation failed.org.hibernate.MappingException: Repeated column in mapping for entity: org.test.B column: id (should be mapped with insert="false" update="false")

I didn´t manage to fit "insert="false" update="false" " in mapping A or B (DTD is not allowing me and rightfully it seems... if i ignore it the tables are not created)
TIA
marefado


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 11:21 am 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
OK i took a closer look so you have kip as a foreign-key in B?
if só you should not map it as a property but Many-to-one with
update= false and insert= false if i am not mistaking

take a look at :
[url=http://www.hibernate.org/hib_docs/v3/reference/en/html/associations.html#assoc-bidirectional-m21]
one-to-many/many-to-one[/url]


Good luck


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 11:36 am 
Newbie

Joined: Mon May 22, 2006 10:23 am
Posts: 7
Hello, tx for all your help.
I edited the mapping for B as you suggested, i think you were suggeting of something like this(was it?):

<class name="org.test.B" table="B">
<id name="id" column="id" type="integer">
<generator class="identity"/>
</id>
<many-to-one name="kip" column="kip" insert="false" update="false"/>
</class>

Now the error is during hibernate mapping:

org.hibernate.MappingException: An association from the table B refers to an unmapped class: java.lang.String

any clues on this?
Tx a lot
marefado


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 12:08 pm 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
You forgot the class attribute in the many-to-one
It should be like this:
Code:
<many-to-one name="kip" column="kip"
   insert="false" update="false"
   class="org.test.B"/>


Hope it works...


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 12:35 pm 
Newbie

Joined: Mon May 22, 2006 10:23 am
Posts: 7
Ok,
i changed as you sugested (but referenced class A, i think it was what you intended to type)

Ended up with:

<class name="org.test.A" table="A">
<id name="kip"/>
<list name="bs">
<key column="kip" not-null="true" />
<list-index column="id" />
<one-to-many class="org.test.B"/>
</list>
</class>

<class name="org.test.B" table="B">
<id name="id" column="id" type="integer">
<generator class="identity"/>
</id>
<many-to-one class="org.test.A" name="kip" column="kip" insert="false" update="false"/>
</class>

and the error:
ifailed.org.hibernate.MappingException: Repeated column in mapping for entity: org.test.B column: id (should be mapped with insert="false" update="false")

ahhhh hes back! (cant fit update=false,insert=false anywere) :(

Here are the java classes if anyone tries to replicate

public class A{
protected String kip;
protected java.util.List<B> bs;

public A()
{
this.bs = new ArrayList();
}

public A(String kip)
{
this.kip=kip;
}

public String getKip() {
return kip;
}

public void setKip(String kip) {
this.kip = kip;
}

public java.util.List<B> getBs() {
return bs;
}

public void setBs(java.util.List<B> bs) {
this.bs = bs;
}
}


public class B {

public int id;
private String kip;

public B()
{

}

public B(String kip)
{
this.kip = kip;
}

public int getId() {
return id;
}

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

public String getKip() {
return kip;
}

public void setKip(String kip) {
this.kip = kip;
}

}


tx!
marefado


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 22, 2006 2:03 pm 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
Ok finally I found the problem...:)

You can forget my last post and from the mapping in Class B
you make the Many-to-one

Code:
<many-to-one name="kip" column="kip" insert="false" update="false"/>


And in the code from your class B change the type of the property
kip from String to A

Code:
public class B {

public int id;
private A kip;
.........

public A getKip(){
  return kip
}

public void setKip(A kip){
  this.kip = kip
}



Had to see the code for that one...:)

That will do the trick..... worked for me anyway[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 7:37 am 
Newbie

Joined: Mon May 22, 2006 10:23 am
Posts: 7
Hello and tx for the quick help ;) !

Unfortunately i cant change my java code. As i see it hibernate should give me the flexibility and not the other way around...

Nevertheless, i tried to change my test objects as suggested and i still receive the same error:

Repeated column in mapping for entity: org.test.B column: id (should be mapped with insert="false" update="false")

Bottomline:
- I am still unable to have a id of a class as a list-index because i cant fit a insert="false" update="false" on the id tag
:(

Tx for your all help!!
joao


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 8:47 am 
Regular
Regular

Joined: Thu Sep 22, 2005 1:53 pm
Posts: 88
Location: Rio de Janeiro
Ok last try , I tried it your way and that worked for me as well
here is the mapping:

Code:
   <class name="A" table="tb_A">

      <id name="kip" column="kip" type="java.lang.String">
         <generator class="assigned">

         </generator>
      </id>

      <list name="bs" lazy="false" cascade="none">

         <key column="kip"></key>

         <index column="id" />

         <one-to-many class="B" />

      </list>

   </class>


Code:
   <class name="B">

      <id name="id" column="id" type="int">
         <generator class="native">
         </generator>
      </id>

      <property name="kip" type="java.lang.String" update="false"
         column="kip" />

   </class>


My configuration file:

Code:
   <session-factory>
      <!--  properties -->
      <property name="hibernate.dialect">
         org.hibernate.dialect.MySQLDialect
      </property>
      <property name="hibernate.connection.driver_class">
         org.gjt.mm.mysql.Driver
      </property>
      <property name="hibernate.connection.url">
         jdbc:mysql://localhost:3306/test
      </property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">
         123
      </property>
      <property name="show_sql">true</property>
      <property name="hibernate.cglib.use_reflection_optimizer">
         false
      </property>
      <property name="hibernate.connection.autocommit">true</property>

      <mapping resource="A.hbm.xml" />
      <mapping resource="B.hbm.xml" />

   </session-factory>




and the Java code:

Code:
public class A {

   public String kip;
   public List bs = new ArrayList();
   
   public String getKip() {
      return kip;
   }
   public void setKip(String kip) {
      this.kip = kip;
   }

   public List getBs() {
      return bs;
   }
   public void setBs(List bs) {
      this.bs = bs;
   }
   
   
}


Code:
public class B {

   private int Id;
   private String kip;
   

   public int getId() {
      return Id;
   }
   public void setId(int id) {
      Id = id;
   }

   public String getKip() {
      return kip;
   }
   public void setKip(String kip) {
      this.kip = kip;
   }
   
   
}


I tried it, the Sessionfactory started witout any problem I was able to insert new A´s B´s and get the collection with a Criteria

_________________
Don´t forget to rate!


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 23, 2006 11:33 am 
Newbie

Joined: Mon May 22, 2006 10:23 am
Posts: 7
Very Well, perfect!
it works now,
Intrigued with the cause of the problem i started coding from your mappings towards my own's...

I found that the responsible for the problem was a not-null="true" on the definition of the list Key(or so it seems):

<class name="org.test.A" table="A">
<id name="kip"/>
<list name="bs">
<key column="kip" not-null="true" />
<list-index column="id" />
<one-to-many class="org.test.B"/>
</list>
</class>

can someone explain the reason for this behaviour?

Many tx for all your help!!!


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