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