Hello,
I'm trying to load a set with costum sql, but I really can't get it working. Below I've posted a copy of all my simplified code(If you copy-paste everything to you're computer and configure an mysql schema you should be able to run it). I really can't see what is caussing the "org.hibernate.MappingException: Unknown collection role: Team.matches".
any help is greatly appriciated.
Hibernate version:3.1.1
Mysql 5.0.16
The Team class code
Code:
package test;
import java.util.Set;
public class Team {
Long id;
String name;
Set matches;
public Long getId() {return id;}
public void setId(Long id){this.id = id;}
public String getName() {return name;}
public void setName(String name){this.name = name;}
public Set getMatches(){return matches;}
public void setMatches(Set matches){this.matches = matches;}
}
The Team class 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="test.Team" table="teams">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="matches" inverse="true">
<key/>
<one-to-many class="test.Match"/>
<loader query-ref="matches"/>
</set>
</class>
<sql-query name="matches">
<load-collection alias="mat" role="Team.matches"/>
SELECT {mat.*} FROM matches mat
WHERE mat.team1 = :id
OR mat.team2 = :id
</sql-query>
</hibernate-mapping>
The Macth codeCode:
package test;
public class Match {
Long id;
Team team1;
Team team2;
String result;
public Long getId() {return id;}
public void setId(Long id){this.id = id;}
public Team getTeam1() {return team1;}
public void setTeam1(Team team1) {this.team1 = team1;}
public Team getTeam2(){return team2;}
public void setTeam2(Team team2){this.team2 = team2;}
public String getResult(){return result;}
public void setResult(String result){this.result = result;}
}
The Match mappingCode:
<?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="test.Match" table="matches">
<id name="id" column="id">
<generator class="native"/>
</id>
<many-to-one name="team1"/>
<many-to-one name="team2"/>
<property name="result"/>
</class>
</hibernate-mapping>
My hibernate.cfg.xml(bassicly copied from the ref manual)Code:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">paswoord</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="test/Team.hbm.xml"/>
<mapping resource="test/Match.hbm.xml"/>
</session-factory>
</hibernate-configuration>
My main class (just try to get an SessionFactory)Code:
package test;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibTestMain {
public static void main(String[] args){
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
}
}
The error and log resulting from executing hibTestMain
Code:
.......
18:45:17,656 DEBUG SchemaExport:296 - alter table matches add index FK321E8933BD79BFCE (team2), add constraint FK321E8933BD79BFCE foreign key (team2) references teams (id)
18:45:17,671 INFO SchemaExport:202 - schema export complete
18:45:17,671 DEBUG DriverManagerConnectionProvider:129 - returning connection to pool, pool size: 1
18:45:17,671 DEBUG SessionFactoryImpl:353 - Checking 0 named HQL queries
18:45:17,671 DEBUG SessionFactoryImpl:373 - Checking 1 named SQL queries
18:45:17,671 DEBUG SessionFactoryImpl:381 - Checking named SQL query: matches
18:45:17,671 DEBUG QueryPlanCache:111 - unable to locate native-sql query plan in cache; generating (SELECT {mat.*} FROM matches mat)
18:45:17,687 ERROR SessionFactoryImpl:336 - Error in named query: matches
org.hibernate.MappingException: Unknown collection role: Team.matches
at org.hibernate.impl.SessionFactoryImpl.getCollectionPersister(SessionFactoryImpl.java:521)
at org.hibernate.loader.custom.SQLQueryReturnProcessor.addCollection(SQLQueryReturnProcessor.java:136)
at org.hibernate.loader.custom.SQLQueryReturnProcessor.processCollectionReturn(SQLQueryReturnProcessor.java:172)
at org.hibernate.loader.custom.SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:101)
at org.hibernate.loader.custom.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:87)
at org.hibernate.loader.custom.SQLCustomQuery.<init>(SQLCustomQuery.java:105)
at org.hibernate.engine.query.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:21)
at org.hibernate.engine.query.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:113)
at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:409)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:327)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
at test.HibTestMain.main(HibTestMain.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:86)
Exception in thread "main" org.hibernate.HibernateException: Errors in named queries: matches
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:338)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
at test.HibTestMain.main(HibTestMain.java:9)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:86)
Process finished with exit code 1