-->
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.  [ 14 posts ] 
Author Message
 Post subject: org.hibernate.MappingException: Unknown entity:
PostPosted: Wed Feb 04, 2009 9:23 am 
Beginner
Beginner

Joined: Wed Jan 28, 2009 10:16 am
Posts: 37
Hello !!

I try to launch a simple test using annotation and hibernate 3 but this error appears.

In fact, I succeed to connect my application with the data base and get all data from the simple table "Adresse" but when I try to add a new one, it fails...

Can you help me ?

This is my code for launch :
Code:
public static void getAllAddresse() {
      /*  Query q = HibernateUtil.getSession().getNamedQuery("adresse.all");
        List results = q.list();
        System.out.println(results);*/
       Session session = HibernateUtil.getSession();
       List results = session.createSQLQuery("SELECT * FROM Adresse").list();
       System.out.println(results);
       Adresse adresse = new Adresse();
       Transaction transaction = session.beginTransaction();
       session.save(adresse);
       transaction.commit();
       session.close();
       }


With the namequery annotation it does not work anymore...

And this is my class Adresse :
Code:
import java.io.Serializable;

/**
* @Entity
* @NamedQuery(name="adresse.all", query="SELECT * FROM Adresse")
*/
public class Adresse implements Serializable{

   private long id;

   /**
    * @Transient
    */
   private String adresse;
   /**
    * @Transient
    */
   private String codePostal;
   /**
    * @Transient
    */
   private String ville;
   
   public Adresse() {
   }

   

   public String getAdresse() {
      return adresse;
   }

   public String getCodePostal() {
      return codePostal;
   }


   /**
    * @Id
    * @GeneratedValue(strategy=GenerationType.AUTO)
    * @return
    */
   public long getId() {
      return id;
   }

...


And finaly, this is my HibernateUtil class :

Code:

public class HibernateUtil {
   private static final SessionFactory sessionFactory;
   
   static {
      try {
         sessionFactory = new AnnotationConfiguration()
            .addAnnotatedClass(Adresse.class)         
             .buildSessionFactory();         
      } catch (Throwable ex) {
         throw new ExceptionInInitializerError(ex);
      }
   }
   
   public static Session getSession() throws HibernateException {      
      return sessionFactory.openSession();
   }
   
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 9:32 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
You commented out your Annotations. Also, in HQL you don't use "Select *".

Code:
/**
* @Entity
* @NamedQuery(name="adresse.all", query="SELECT * FROM Adresse")
*/
public class Adresse

should be
Code:
  @Entity
  @NamedQuery(name="adresse.all", query="FROM Adresse")
  public class Adresse


Then you don't need your SQLQuery anymore.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 10:01 am 
Beginner
Beginner

Joined: Wed Jan 28, 2009 10:16 am
Posts: 37
Thanks for the comment !!!

But for the query I always have the following error :
Code:
org.hibernate.MappingException: Named query not known: adresse.all


This is my Adresse class now :
Code:
import java.io.Serializable;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;

import org.hibernate.annotations.Entity;
import org.hibernate.annotations.NamedQuery;

@Entity
@NamedQuery(name = "adresse.all", query = "FROM Adresse")
public class Adresse implements Serializable {

   private int id;

   @Transient
   private String adresse;
   @Transient
   private String codePostal;
   @Transient
   private String ville;

   public Adresse() {
   }

   public String getAdresse() {
      return adresse;
   }

   public String getCodePostal() {
      return codePostal;
   }

   /**
    *
    * @return
    */
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   public int getId() {
      return id;
   }


And when I use the query :

Code:

Query q = HibernateUtil.getSession().getNamedQuery("adresse.all");
        List results = q.list();
        System.out.println(results);


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 10:09 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Change
Code:
import org.hibernate.annotations.Entity;
import org.hibernate.annotations.NamedQuery;

to
Code:
import javax.persistence.Entity;
import javax.persistence.NamedQuery;


hibernates org.hibernate.annotations.Entity is only a complement to javax.persistence.Entity.

Rating is welcome. ;-)

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 10:39 am 
Beginner
Beginner

Joined: Wed Jan 28, 2009 10:16 am
Posts: 37
Thanks for this details. But I always have the same error ... :( ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 10:43 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Could you post your complete changed code?

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 11:32 am 
Beginner
Beginner

Joined: Wed Jan 28, 2009 10:16 am
Posts: 37
Ok,

This my Adresse class :
Code:
package model.hibernate;

import java.io.Serializable;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Transient;

import javax.persistence.Entity;
import javax.persistence.NamedQuery;

@Entity
@NamedQuery(name="adresse.all", query="FROM Adresse")
public class Adresse implements Serializable {

   private int id;

   @Transient
   private String adresse;
   @Transient
   private String codePostal;
   @Transient
   private String ville;

   public Adresse() {
   }

   public String getAdresse() {
      return adresse;
   }

   public String getCodePostal() {
      return codePostal;
   }

   /**
    *
    * @return
    */
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   public int getId() {
      return id;
   }

   public String getVille() {
      return ville;
   }

   public void setAdresse(String adresse) {
      this.adresse = adresse;
   }

   public void setCodePostal(String codePostal) {
      this.codePostal = codePostal;
   }

   private void setId(int id) {
      this.id = id;
   }

   public void setVille(String ville) {
      this.ville = ville;
   }
}


This is my HibernateUtil class :
Code:
package control.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

import model.hibernate.Adresse;



public class HibernateUtil {
   private static final SessionFactory sessionFactory;
   
   static {
      try {
         Configuration annotationConfiguration = new AnnotationConfiguration()
         //.configure("hibernate.cfg.xml");
         //.addFile("src/hibernate.cfg.xml")
         .addPackage("model.hibernate"); // le nom complet du package
            //.addAnnotatedClass(Adresse.class); 
            // .addResource("test/animals/orm.xml")*/
             sessionFactory = annotationConfiguration.configure().buildSessionFactory();   
         
// to test the file on export because it is empty...
         SchemaExport export = new SchemaExport(annotationConfiguration);
         export.setOutputFile("test");
         export.create(true, true);
         export.drop(true, true);
      } catch (Throwable ex) {
         throw new ExceptionInInitializerError(ex);
      }
   }
   
   public static Session getSession() throws HibernateException {      
      return sessionFactory.openSession();
   }
   
}



This is my main code :
Code:

Query q = HibernateUtil.getSession().getNamedQuery("adresse.all");
        List results = q.list();
        System.out.println(results);
       Session session = HibernateUtil.getSession();
       Adresse adresse = new Adresse();
       Transaction transaction = session.beginTransaction();
       session.save(adresse);
       transaction.commit();
       session.close();



This is my hibernates.properties files :
Code:
hibernate.dialect org.hibernate.dialect.PostgreSQLDialect
hibernate.connection.driver_class org.postgresql.Driver
hibernate.connection.url jdbc:postgresql:MyDB
hibernate.connection.username postgres
hibernate.connection.password bla
hibernate.connexion.pool_size 1
hibernate.show_sql true
hibernate.hbm2ddl.auto create-drop


This is my hibernate.cfg.xml file :
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.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
       <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
       <property name="hibernate.connection.url">jdbc:postgresql:MyDB</property>
       <property name="hibernate.connection.username">postgres</property>
       <property name="hibernate.connection.password">bla</property>
       <property name="hibernate.connexion.pool_size">1</property>
       <property name="hibernate.show_sql">true</property>
       <property name="hibernate.hbm2ddl.auto">create-drop</property>




    </session-factory>

</hibernate-configuration>


And I think that all...


Last edited by Louevie on Mon Feb 09, 2009 7:09 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 11:45 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
I think the problem is in line:
Code:
Configuration annotationConfiguration = new AnnotationConfiguration()
         .addPackage("fr.alliance.expert.tma.model.hibernate");

Your class is not in this package, its in model.hibernate. So the whole entity is not mapped, including its NamedQuery.

Rating appreciated.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 12:15 pm 
Beginner
Beginner

Joined: Wed Jan 28, 2009 10:16 am
Posts: 37
sorry, in fact is it possible to edit a post because I remplace this package by
model.hibernate to not include the name of the project but I assure that in my own code, it is the same package...
I try with .addAnnotatedClass(Adresse.class); and it works ;)

Just another error... I look this now ;)

Thank you very much, your my day's angel ;)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 12:17 pm 
Beginner
Beginner

Joined: Wed Jan 28, 2009 10:16 am
Posts: 37
In fact, it is very strange, now, the log indicates that
org.hibernate.exception.SQLGrammarException: could not execute query
because the relation adresse does not exist and when I see my data base, I am very surprise because the table disapears ???


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 12:38 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
It disappears because you have set hibernate.hbm2ddl.auto to create-drop. Set it to update or create instead. Create-drop means, that hibernate should create all tables and drop it at the end of your application.

Your query does not work, because your mapping is wrong. You have annotated your id at the get-method, but @Transient at the fields. You must not mix it, change it for example to:
Code:
@Entity
@NamedQuery(name="adresse.all", query="FROM Adresse")
public class Adresse implements Serializable {
 
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   private int id;

   @Transient
   private String adresse;
   @Transient
   private String codePostal;
   @Transient
   private String ville;

   public Adresse() {
   }

   public String getAdresse() {
      return adresse;
   }

   public String getCodePostal() {
      return codePostal;
   }

   
   public int getId() {
      return id;
   }
...


Please rate my posts, as I think I could help you!

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 12:52 pm 
Beginner
Beginner

Joined: Wed Jan 28, 2009 10:16 am
Posts: 37
Thanks you, I seach the traduction for drop and it is the problem. Now it works.
I have not change the Id annotation and it works but I do not understand. On hibernate site it is said to put annotation on the getter ?

Is there some rule ?

http://www.hibernate.org/hib_docs/annot ... index.html

It works !!! I am happy ! Thanks you !!!!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 1:02 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Quote:
On hibernate site it is said to put annotation on the getter ?

Is there some rule ?

Well, you can also annotate you Getters, but you have to do the same for all other mapping-annotations. You cannot mix it, by putting @Id to the Getter and @Transient to the Field. All Annotations have to be where the @Id-Annotation is.

Quote:
It works !!! I am happy ! Thanks you !!!!

By the way, you can rate helpful posts, to let others see, which posts actually helped to solve a problem.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 05, 2009 4:36 am 
Beginner
Beginner

Joined: Wed Jan 28, 2009 10:16 am
Posts: 37
Ok, thank you for all !


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