-->
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.  [ 9 posts ] 
Author Message
 Post subject: Base question
PostPosted: Fri Feb 10, 2006 9:03 am 
Beginner
Beginner

Joined: Fri Feb 10, 2006 8:33 am
Posts: 32
Hi GURUS.
I have base question.

There is 2 classes
Code:
<hibernate-mapping>
        <class name="model.Classification" table="classification">
                <id name="id" column="id">
                       
                        <generator class="sequence">
                              <param name="sequence">test</param>
                        </generator>
                </id>
                 <set name="ObjectRelation_test" lazy="true" inverse="true" cascade="all-delete-orphan">
                    <key column="id_object1"/>
                   
                    <one-to-many class="model.ObjectRelation_test" />
                </set>

  </class>
</hibernate-mapping>

and

<hibernate-mapping>
        <class name="model.ObjectRelation_test" table="object_relations" where="code_object1=1 and code_object2=1">
                <id name="id" column="id">
                   
                        <generator class="sequence">
                                <param name="sequence">test</param>
                        </generator>
                </id>
               
                <property name="code_object1" not-null="true"/>
                <property name="role"/>
               
        </class>
</hibernate-mapping>


And there is:

Code:
Classification cls=(Classification)session.load(Classification.class,new Long(1));

Set mySet=cls.getObjectRelation_test();

ObjectRelation_test obr=new ObjectRelation_test();

obr.setRole(new Long(22));

mySet.add(obr);

So tell me how to set automaticly value ot
Code:
code_object1
in
Code:
ObjectRelation_test
to be equal of
Code:
Classification.id


In this moment when create commit, there is
Code:
Cannot insert a null into column (object_relations.id_object1)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 10:53 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Hello,

this is a simple 1:n relation. Have a look in the hibernate reference. Your second mapping is not correct.

Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 11:08 am 
Beginner
Beginner

Joined: Fri Feb 10, 2006 8:33 am
Posts: 32
Hello LaliLuna

Yes I know that. I looked in refference, Hibernate in Action and in Hibernate Quickly and can't find solution. I don't have to create one-to-many and many-to-one relation . just one-to-many.

Tell me please how to create this.

Regards

LaLiLuna wrote:
Hello,

this is a simple 1:n relation. Have a look in the hibernate reference. Your second mapping is not correct.

Sebastian


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 11:10 am 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Then you must not use
<property name="code_object1" not-null="true"/>

just delete it.

Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 12:15 pm 
Beginner
Beginner

Joined: Fri Feb 10, 2006 8:33 am
Posts: 32
There is small mistake
<hibernate-mapping>
<class name="model.Classification" table="classification">
<id name="id" column="id">

<generator class="sequence">
<param name="sequence">test</param>
</generator>
</id>
<set name="ObjectRelation_test" lazy="true" inverse="true" cascade="all-delete-orphan">
<key column="id_object1"/>

<one-to-many class="model.ObjectRelation_test" />
</set>

</class>
</hibernate-mapping>

and

<hibernate-mapping>
<class name="model.ObjectRelation_test" table="object_relations" where="code_object1=1 and code_object2=1">
<id name="id" column="id">

<generator class="sequence">
<param name="sequence">test</param>
</generator>
</id>

<property name="id_object1" not-null="true"/>
not <property name="code_object1" not-null="true"/>
<property name="role"/>

</class>
</hibernate-mapping>

an if delete <property name="id_object1" not-null="true"/>
result is:
Cannot insert a null into column (object_relations.id_object1)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 12:23 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
krasig
you must decide if you want to have a bi-directional relation
1:n + n:1 or uni-directional.
Either you omit the tag as I told you and have a 1:n
or you must correct it as explained in 1:n+n:1 relation in the reference.

I would appreciate if you rate, when this solved your problem

Sebastian

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 12:29 pm 
Beginner
Beginner

Joined: Fri Feb 10, 2006 8:33 am
Posts: 32
Hi LaliLuna

I have to create 1:n uni-directional relation.
But when save Parent objectm, must automaticly optain ID to save in Chaild table.
And tell me how to create this simple operation

P.S please excuse me for bad language



LaLiLuna wrote:
krasig
you must decide if you want to have a bi-directional relation
1:n + n:1 or uni-directional.
Either you omit the tag as I told you and have a 1:n
or you must correct it as explained in 1:n+n:1 relation in the reference.

I would appreciate if you rate, when this solved your problem

Sebastian


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 12:40 pm 
Expert
Expert

Joined: Tue Nov 23, 2004 7:00 pm
Posts: 570
Location: mostly Frankfurt Germany
Hopefully I will get a rating ;-)

first:
no inverse="true" and not null as well
just
Code:
                 <set name="ObjectRelation_test" lazy="true"  cascade="all-delete-orphan">
                    <key column="id_object1"  not-null="true"/>
                   
                    <one-to-many class="model.ObjectRelation_test" />
                </set>



Pleeeaaaaase delete
Code:
<property name="id_object1" not-null="true"/>

then your code will work. Do not call session.save(myObjectRelation_test);
just add it to the collection of your classification and commit the transaction.

_________________
Best Regards
Sebastian
---
Training for Hibernate and Java Persistence
Tutorials for Hibernate, Spring, EJB, JSF...
eBook: Hibernate 3 - DeveloperGuide
Paper book: Hibernate 3 - Das Praxisbuch
http://www.laliluna.de


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 12:48 pm 
Beginner
Beginner

Joined: Fri Feb 10, 2006 8:33 am
Posts: 32
Yeaaaaaaaaaaaa.That was a great.
Many thanks LaLiLuna

God bless you.


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