2016-08-24 60 views
2

我試圖創建休眠一個小項目,但我得到的錯誤「類型沒有被映射[從O型選擇o]」我在Hibernate映射添加.cfg.xml但仍然錯誤。Enity沒有映射[選擇O型O]

Type.java:

package com.formation.gestionprojet.doa.entity; 

import java.io.Serializable; 

import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table; 


@Entity 
@Table(name="Type") 
public class Type implements Serializable { 
/** 
* 
*/ 
private static final long serialVersionUID = 1L; 

@Id 
private Long id; 

private String name; 

private String description; 

private String active; 




public Type() { 
    super(); 
    // TODO Auto-generated constructor stub 
} 

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 String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public String getActive() { 
    return active; 
} 

public void setActive(String active) { 
    this.active = active; 
} 

} 

hibernate.org.xml:

<?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 setting --> 

<property name ="connection.driver_class">com.mysql.jdbc.Driver</property> 
<property name="connection.url">jdbc:mysql://localhost:3306/gestion_projet?createDatabaseIfNotExist=true</property> 
<property name="connection.username">root</property> 
<property name= "connection.password">root</property> 

<!-- Dialect --> 
<property name="dialect">org.hibernate.dialect.MySQLDialect</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> 

<!-- Drope and re-create the database --> 
<property name="hbm2ddl.auto">update</property> 

<!-- mapping --> 


<mapping class= "com.formation.gestionprojet.doa.entity.Type"/> 


</session-factory> 

</hibernate-configuration> 

hibernateUtil.java:

package com.formation.gestionprojet.utils; 
import org.hibernate.HibernateException; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry; 
import org.hibernate.service.ServiceRegistryBuilder; 

@SuppressWarnings("deprecation") 
public class HibernateUtil 
{ 
    private static SessionFactory sessionFactory; 
    private static ServiceRegistry serviceRegistry; 

    static 
    { 
     try 
     { 

      Configuration configuration = new Configuration(); 
      configuration.configure("config/hibernate.cfg.xml"); 

      serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 
      sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
     } 
     catch (HibernateException ex) 
     { 
      System.err.println("Error creating Session: " + ex); 
      throw new ExceptionInInitializerError(ex); 
     } 
    } 

    public static SessionFactory getSessionFactory() 
    { 
     return sessionFactory; 
    } 



    public static Session openSession() 
    { 
     return sessionFactory.openSession(); 
    } 


    public static Session getCurrentSession() 
    { 
     return sessionFactory.getCurrentSession(); 
    } 


    public static void close(){ 
     if(sessionFactory!=null){ 
      sessionFactory.close(); 
     } 

    } 


} 

Test.Java

package com.formation.gestionprojet.utils; 

import org.hibernate.Session; 



public class Test { 

    static Session session = HibernateUtil.openSession(); 

    public static void main(String[] args) { 

     session.createQuery("select o from Type o").list(); 



    } 

} 
+1

'Type'是在MySQL的保留字。嘗試爲您的實體和表使用不同的名稱。 –

+0

感謝您的回答,還是一樣的錯誤 – sacamoto

+0

任何建議,請! – sacamoto

回答

0

解決!問題在於我使用的是hibernate的版本,因此我改變了它,HibernateUtil中的更改。