Hello,
I am trying to map a class in Hibernate with XML, but it seems to work only when querying with HQL, the save and get methods on the other hand don't work. Also with annotations everything works flawlessly. So, when I try to call the get(MyClass.class, id) method, the following exception is thrown:
Code:
Exception in thread "main" org.hibernate.MappingException: Unknown entity: pakagename.MyClass
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:92)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:1090)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:978)
at packagename.test.StaticTest.main(StaticTest.java:11)
I am using the following configuration:
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>
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost:1433/MyDatabase</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">password</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<mapping resource="packagename/MyClass.hbm.xml" />
</session-factory>
</hibernate-configuration>
for the MyClass class I have:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class entity-name="MyClass"
name="packagename.MyClass" table="MyClass"
dynamic-insert="true" dynamic-update="true">
<id name="myClassSid" type="integer">
<column name="MyClassID" />
<generator class="assigned" />
</id>
<property name="myClassName" type="string">
<column length="256" name="MyClassName" not-null="true" />
</property>
</class>
</hibernate-mapping>
MyClass.java:
Code:
package pakagename.MyClass;
public class MyClass implements java.io.Serializable {
private Integer myClassSid;
private String myClassName;
public MyClass() {
}
// setters, getters
}
I have a table named MyClass with two columns MyClassID(pk) and MyClassName(varchar(255))
Finally:
Code:
MyClass myClass = (MyClass) session.creteQuery("from MyClass where myClassSid = :myClassSid)
.setInteger("myClassSid", id).uniqueResult();
-- works OK
Code:
MyClass myClass = (MyClass) session.get(MyClass.class, id);
-- throws the above listed exception
How can HQL queries work perfectly OK but get and save methods not? As I said with annotation the same configuration works fine.
I would really appreciate if someone can give some suggestions.
Thanks.