I have another thread with the same problem, but I'm trying a different solution. Therefor this thread.
The other:
viewtopic.php?f=1&t=1003721Situation: I have 3 classes. Country, Language and CountryName
Country and Language have an id (assigned) and some unrelevant stuff.
CountryName has a many-to-one to Country, a many-to-one to Language and a name (String).
The problem is in CountryName. The two many-to-one's are used as a composite key, so I've tried to map them as a composite id. But I'm still unlucky.
Can someone help me with this problem or give me a example of a similar (working) structure?
Note: I'm confident the problem is in the mapping of CountryNames, but I've added the entire code.
The mappings:
Code:
<class name="data.Country" table="countries">
<id name="id" column="id" length="31">
<generator class="assigned" />
</id>
<set name="countryNames" table="countryNames" inverse="true" cascade="delete">
<key column="country" />
<one-to-many class="data.CountryName" />
</set>
</class>
Code:
<class name="data.Language" table="languages">
<id name="id" column="id" length="31">
<generator class="assigned" />
</id>
<set name="countryNames" table="countryNames" inverse="true" cascade="delete">
<key column="language" />
<one-to-many class="data.CountryName" />
</set>
</class>
Code:
<class name="data.CountryName" table="countryNames">
<composite-id name="countryLanguageFK" class="data.CountryLanguageFK">
<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" />
</class>
These are my java classes:
Code:
public class Country
{
private String id;
private Set<CountryName> countryNames = new HashSet<CountryName>(0);
public Country()
{
}
/* Getters */
public String getId()
{
return id;
}
public Set<CountryName> getCountryNames()
{
return countryNames;
}
/* Setters */
private void setId(String id)
{
this.id = id;
}
public void setCountryNames(Set<CountryName> countryNames)
{
this.countryNames = countryNames;
}
}
Code:
public class Language
{
private String id;
private Set<CountryName> countryNames = new HashSet<CountryName>(0);
public Language()
{
}
/* Getters */
public String getId()
{
return id;
}
public Set<CountryName> getCountryNames()
{
return countryNames;
}
/* Setters */
private void setId(String id)
{
this.id = id;
}
public void setCountryNames(Set<CountryName> countryNames)
{
this.countryNames = countryNames;
}
}
Code:
public class CountryName
{
private CountryLanguageFK countryLanguageFK;
private String name;
public CountryName()
{
}
/* Getters */
public CountryLanguageFK getCountryLanguageFK()
{
return countryLanguageFK;
}
public Country getCountry()
{
return countryLanguageFK.getCountry();
}
public Language getLanguage()
{
return countryLanguageFK.getLanguage();
}
public String getName()
{
return name;
}
/* Setters */
public void setCountryLanguageFK(CountryLanguageFK countryLanguageFK)
{
this.countryLanguageFK = countryLanguageFK;
}
public void setCountry(Country country)
{
countryLanguageFK.setCountry(country);
}
public void setLanguage(Language language)
{
countryLanguageFK.setLanguage(language);
}
public void setName(String name)
{
this.name = name;
}
}
class CountryLanguageFK implements Serializable
{
private Country country;
private Language language;
public CountryLanguageFK(Country country, Language language)
{
this.country = country;
this.language = language;
}
public Country getCountry()
{
return country;
}
public Language getLanguage()
{
return language;
}
public void setCountry(Country country)
{
this.country = country;
}
public void setLanguage(Language language)
{
this.language = language;
}
/* Override */
public boolean equals(Object o)
{
if (o instanceof CountryLanguageFK)
{
CountryLanguageFK other = (CountryLanguageFK) o;
return new EqualsBuilder().append(country, other.getCountry()).append(language, other.getLanguage()).isEquals();
}
else
return false;
}
public int hashCode()
{
return new HashCodeBuilder().append(country).append(language).toHashCode();
}
}
And this is the error I receive:
Code:
org.hibernate.QueryException: could not resolve property: country of: data.CountryName
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.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1402)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:54)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1377)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:457)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:417)
at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:68)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:357)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:113)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:82)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:91)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1577)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:306)
at main.HibernateMain.main(HibernateMain.java:27)