-->
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: [Hibernate]Master-details mapping?
PostPosted: Wed Apr 20, 2005 1:20 pm 
Newbie

Joined: Wed Apr 20, 2005 1:06 pm
Posts: 8
Supposed I have two tables
(It's "pseudo-code" schema of what I have now)
[Address]
*Street
*City
*State(like, "NY","CA","PA")
*Zip
(Primary Key: Street+City, well, it sounds silly, but again, it's pseudo)

[States]
*StateID(like,"NY","CA","PA")
*StateName(like, "New York", "Califonia")
[Primary Key: StateID]

in Address.hbm.xml, I have
---------------------------------------------------------------------
[as below]
...
----------------------------------------------

in Address.java, I have
---------------------------------------
...
private Set state;
public void setState(Set state){
this.state=state;
}

public Set getState(){
return state;
}
...
---------------------------------------

******************************************
What I am trying to do is to add a method getStateName() in Address.java
public String getStateName(String stateid){
Iterator it = state.iterator();
while(it.hasNext()){
State state= (State)it.next();
if(state.getStateID().equalsIgnoreCase(stateid)){
return state.getStateName();
}
}
return "";
}

I didn't define any foreign key constraint between this two tables,
(Acutally, this is what exactly the situation in my legacy database
schema,and I can't change that, it sounds weird,but it has to be in that way)

I am trying to test the class, but it always has an exception says
[as below]

I am new to Hibernate, does anyone can tell me what's missing in here?
or I map these 2 tables in a wrong strategy? or, I should not
use hibernate at all if there is Foreign Keys no constarint defined?

Regards

Patrick

Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:2.1.7

Mapping documents:Address.hmb.xml
...
<composite-id>
<key-property name="street" column="street" type="java.lang.String"
<key-property name="city" column="city" type="java.lang.String"
</composite-id>
<property name="state" column="state" type="java.lang.String"/>
<property name="zip" column="zip" type="java.lang.Integer"/>

<set name="state"
cascade="all-delete-orphan"
inverse="true"
lazy="true"
<key>
<column name="StateID"/>
</key>
<one-to-many class="State"/>
</set>
...


Code between sessionFactory.openSession() and session.close():
Using the HibernateUtil.java in sample application CaveatEmptor


Full stack trace of any exception that occurs:
- Hibernate 2.1.7
- hibernate.properties not found
- using CGLIB reflection optimizer
- using JDK 1.4 java.sql.Timestamp handling
- configuring from resource: /hibernate.cfg.xml
- Configuration resource: /hibernate.cfg.xml
- Mapping resource: com/mycompany/hibernate/model/Address.hbm.xml
- Mapping class: com.mycompany.hibernate.model.Address -> Address
- Mapping resource: com/mycompany/hibernate/model/State.hbm.xml
- Mapping class: com.mycompany.hibernate.model.State -> State
- Configured SessionFactory: null
- processing one-to-many association mappings
- Mapping collection: com.mycompany.hibernate.model.Address.Address -> Address
- Mapping collection: com.mycompany.hibernate.model.Address.State -> State
- processing one-to-one association property references
- processing foreign key constraints
- Building SessionFactory failed.
net.sf.hibernate.MappingException: collection foreign key mapping has wrong number of columns: com.mycompany.hibernate.model.Address.State type: com.mycompany.hibernate.model.AddressKey
at net.sf.hibernate.mapping.Collection.validate(Collection.java:248)
at net.sf.hibernate.cfg.Configuration.validate(Configuration.java:626)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:790)
at com.mycompany.hibernate.util.HibernateUtil.<clinit>(HibernateUtil.java:33)
at com.mycompany.hibernate.model.dao.AddressDAO.<init>(AddressDAO.java:22)
at test.com.mycompany.hibernate.model.TestAddressDAO.main(TestAddressDAO.java:25)
java.lang.ExceptionInInitializerError
at com.mycompany.hibernate.util.HibernateUtil.<clinit>(HibernateUtil.java:40)
at com.mycompany.hibernate.model.dao.AddressDAO.<init>(AddressDAO.java:22)
at test.com.mycompany.hibernate.model.TestAddressDAO.main(TestAddressDAO.java:25)
Caused by: net.sf.hibernate.MappingException: collection foreign key mapping has wrong number of columns: com.mycompany.hibernate.model.Address.State type: com.mycompany.hibernate.model.AddressKey
at net.sf.hibernate.mapping.Collection.validate(Collection.java:248)
at net.sf.hibernate.cfg.Configuration.validate(Configuration.java:626)
at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:790)
at com.mycompany.hibernate.util.HibernateUtil.<clinit>(HibernateUtil.java:33)
... 2 more
Exception in thread "main"


Name and version of the database you are using:
SQL Server 2000


The generated SQL (show_sql=true):n/a

Debug level Hibernate log excerpt: Debug


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 5:26 am 
Beginner
Beginner

Joined: Wed Apr 21, 2004 8:33 am
Posts: 27
Hi
The Mapping is wrong. It should be many-to-one mapping.
You can just give this mapping and when do .get () of the StateID
it will give the statename.

Just try it out. By the way it didnt put the other xml file


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 21, 2005 11:54 am 
Newbie

Joined: Wed Apr 20, 2005 1:06 pm
Posts: 8
yvijaykumar wrote:
Hi
The Mapping is wrong. It should be many-to-one mapping.
You can just give this mapping and when do .get () of the StateID
it will give the statename.

Just try it out. By the way it didnt put the other xml file


Got it solved, thanks a lot


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.