-->
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: Hibernate xml mapping working with HQL but not with get
PostPosted: Tue Nov 15, 2011 9:41 am 
Newbie

Joined: Tue Nov 15, 2011 8:55 am
Posts: 2
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.


Last edited by urtimudcinul on Tue Nov 15, 2011 3:55 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject: Re: Hibernate xml mapping working with HQL but not with get
PostPosted: Tue Nov 15, 2011 3:53 pm 
Newbie

Joined: Tue Nov 15, 2011 8:55 am
Posts: 2
Solved the issue by naming entity-name attribute with the full class name in the MyClass.hbm.xml:
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="packagename.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>


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.