-->
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.  [ 2 posts ] 
Author Message
 Post subject: Mapping cross-tables
PostPosted: Tue Mar 16, 2010 6:16 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
I'm having troubles getting an object through a cross table.

I have 3 tables: countries, languages, countryNames.
countries and languages just have a PK.
countryNames has a country, language, and a name.

I can get the countryName by using "WHERE country = 'countryId' AND language = 'languageId'".
But when I try countryName.getCountry it returns null. The same goes for language, but getName does work. So I quess it's a problem in my mapping file from countryName.

Here are the 3 mapping files:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="data.Country" table="countries">
      <id name="id" column="id" length="20">
         <generator class="assigned" />
      </id>
   </class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="data.Language" table="languages">
      <id name="id" column="id" length="10">
         <generator class="assigned" />
      </id>

      <property name="active">
         <column name="active" not-null="true" />
      </property>
   </class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
   <class name="data.CountryName" table="countryNames">
      <composite-id name="countryNameFK" class="data.CountryNameFK">
         <key-many-to-one name="country" class="data.Country" column="country" />
         <key-many-to-one name="language" class="data.Language" column="language" />
      </composite-id>

      <property name="name">
         <column name="name" length="50" not-null="true" />
      </property>
   </class>
</hibernate-mapping>

I suspect I do something wrong in the <composite-id>.
Can someone tell me how it should be?


Top
 Profile  
 
 Post subject: Re: Mapping cross-tables
PostPosted: Tue Mar 16, 2010 11:25 am 
Regular
Regular

Joined: Wed Mar 10, 2010 4:48 am
Posts: 106
It turns out I have this problem with al my Foreign Keys in my cross tables.

Does anyone knows what I'm doing wrong, or can somone give an example of a cross-table?

Edit:
For example, when I execute the following code:
Code:
String SQL_QUERY = "FROM Country c WHERE c.id IN (SELECT n.country FROM CountryName n)";
Query query = SessionHandler.getSession().createQuery(SQL_QUERY);
Iterator<Country> it = query.iterate();
if (it.hasNext())
{
   System.out.println(it.next().getId());
}
I get this error:
Code:
Exception in thread "main" org.hibernate.QueryException: could not resolve property: country of: data.CountryName [FROM data.Country c WHERE c.id IN (SELECT n.country FROM data.CountryName n)]
   at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:67)
   at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:61)
   at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1385)
   at org.hibernate.hql.ast.tree.FromElementType.getPropertyType(FromElementType.java:302)
   at org.hibernate.hql.ast.tree.FromElement.getPropertyType(FromElement.java:407)
   at org.hibernate.hql.ast.tree.DotNode.getDataType(DotNode.java:589)
   at org.hibernate.hql.ast.tree.DotNode.prepareLhs(DotNode.java:264)
   at org.hibernate.hql.ast.tree.DotNode.resolve(DotNode.java:211)
   at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:117)
   at org.hibernate.hql.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:113)
   at org.hibernate.hql.ast.tree.DotNode.resolveSelectExpression(DotNode.java:649)
   at org.hibernate.hql.ast.HqlSqlWalker.resolveSelectExpression(HqlSqlWalker.java:761)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExpr(HqlSqlBaseWalker.java:1885)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectExprList(HqlSqlBaseWalker.java:1825)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectClause(HqlSqlBaseWalker.java:1394)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:553)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.collectionFunctionOrSubselect(HqlSqlBaseWalker.java:4253)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.inRhs(HqlSqlBaseWalker.java:4161)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3842)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1762)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:776)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:577)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
   at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
   at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
   at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
   at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
   at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650)
   at main.HibernateMain.main(HibernateMain.java:14)


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