2016-02-27 84 views
1

我努力學習休眠和我在與休眠註釋的問題,事情是,Hibernate並不縫承認我的實體用戶冬眠不承認我的實體

package com.mycompanyname.myapp; 
import java.util.Date; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 


@Entity 
@Table(name="FINANCES_USER") 
public class User { 

@Id 
@GeneratedValue(strategy=GenerationType.IDENTITY) 
@Column(name="USER_ID") 
private Long userId; 

@Column(name="FIRST_NAME") 
private String firstName; 

@Column(name="LAST_NAME") 
private String lastName; 

@Column(name="BIRTH_DATE") 
private Date birthDate; 

@Column(name="EMAIL_ADDRESS") 
private String emailAddress; 

@Column(name="LAST_UPDATED_DATE") 
private Date lastUpdatedDate; 

@Column(name="LAST_UPDATED_BY") 
private String lastUpdatedBy; 

@Column(name="CREATED_DATE") 
private Date createdDate; 

@Column(name="CREATED_BY") 
private String createdBy; 

public Long getUserId() { 
    return userId; 
} 

public void setUserId(Long userId) { 
    this.userId = userId; 
} 

public String getFirstName() { 
    return firstName; 
} 

public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 

public String getLastName() { 
    return lastName; 
} 

public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 

public Date getBirthDate() { 
    return birthDate; 
} 

public void setBirthDate(Date birthDate) { 
    this.birthDate = birthDate; 
} 

public String getEmailAddress() { 
    return emailAddress; 
} 

public void setEmailAddress(String emailAddress) { 
    this.emailAddress = emailAddress; 
} 

public Date getLastUpdatedDate() { 
    return lastUpdatedDate; 
} 

public void setLastUpdatedDate(Date lastUpdatedDate) { 
    this.lastUpdatedDate = lastUpdatedDate; 
} 

public String getLastUpdatedBy() { 
    return lastUpdatedBy; 
} 

public void setLastUpdatedBy(String lastUpdatedBy) { 
    this.lastUpdatedBy = lastUpdatedBy; 
} 

public Date getCreatedDate() { 
    return createdDate; 
} 

public void setCreatedDate(Date createdDate) { 
    this.createdDate = createdDate; 
} 

public String getCreatedBy() { 
    return createdBy; 
} 

public void setCreatedBy(String createdBy) { 
    this.createdBy = createdBy; 
} 

} 

這是我的主

package com.mycompanyname.myapp; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.boot.registry.StandardServiceRegistry; 
import org.hibernate.boot.registry.StandardServiceRegistryBuilder; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.metamodel.MetadataSources; 
import com.mycompanyname.myapp.User; 
public class Application { 

    public static void main(String[] args) { 
     // A SessionFactory is set up once for an application! 
     SessionFactory sessionFactory =null; 
     final StandardServiceRegistry registry = new StandardServiceRegistryBuilder() 
       .configure() // configures settings from hibernate.cfg.xml 
       .build(); 
     try { 
      sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); 
     } 
     catch (Exception e) { 
      // The registry would be destroyed by the SessionFactory, but we had trouble building the SessionFactory 
      // so destroy it manually. 
      StandardServiceRegistryBuilder.destroy(registry); 
     } 

    Session session = sessionFactory.openSession(); 
    session.beginTransaction(); 
    session.getTransaction().commit(); 
    User user1 = new User(); 
    user1.setUserId((long)234); 
    session.save(user1); 
    //session.close(); 
    } 

} 

這是我的hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory name="HibernateUtil"> 
     <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> 
     <property name="hibernate.connection.password">database</property> 
     <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/financialanalysis</property> 
     <property name="hibernate.connection.username">postgres</property> 
     <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> 

     <mapping class="com.mycompanyname.myapp.User"/> 
    </session-factory> 
</hibernate-configuration> 

這個例外不斷飛出

Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.mycompanyname.myapp.User 
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1096) 
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1443) 
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:116) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209) 
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194) 
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49) 
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90) 
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715) 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707) 
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702) 
    at com.mycompanyname.myapp.Application.main(Application.java:32) 

所以我做錯了什麼?

回答

0

我認爲您配置SessionFactory的方式存在問題。我基於您提供的源代碼創建了一個示例應用程序。我懷疑這條線有什麼問題。

sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();

所以我會採取不同的方法來配置會話工廠這樣的。

SessionFactory sessionFactory =null; 

    try { 
     Configuration configuration = new Configuration(); 
     configuration.configure(); 
     final ServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); 
     sessionFactory = configuration.buildSessionFactory(registry); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
     sessionFactory.close(); 
    } 

    Session session = sessionFactory.openSession(); 
    session.beginTransaction(); 
    session.getTransaction().commit(); 
    User user1 = new User(); 
    user1.setUserId((long)234); 
    session.save(user1); 
    session.close(); 

查看源代碼configuration.configure()方法也發現作爲new StandardServiceRegistryBuilder().configure()確實類路徑默認hibernate.cfg.xml文件。有了這些配置,現在您應該可以在沒有任何問題的情況下堅持/加載/更新實體。

我使用Hibernate 4.0測試了一個快速而髒的示例應用程序的配置。

+0

感謝它的工作。 – AchrafElafrit

0

只是用它來建立一個會話工廠

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 

它適用於休眠4和Hibernate 5.此代碼在休眠4棄用,但不贊成在休眠5

+0

我正在與休眠5.1謝謝你這是更簡單:) – AchrafElafrit

+0

@AchrafElafrit不客氣 –

0

刪除配置你的User class like-

Configuration cfg=new Configuration(); 
cfg.addAnnotatedClass(com.mycompanyname.myapp.User.class); 
cfg.configure(); 

希望它有幫助。

+0

是的,我試過,但它似乎有不兼容問題。我使用的是hibernate 5.1和配置對象,它告訴我沒有名稱爲addAnnotatedClass的方法,我將它們移動到了MetadataSources對象 – AchrafElafrit

+0

@AchrafElafrit,請更新您的jar包。 \t hibernate-3.2.6.ga,hibernate-annotations-3.2.1.ga,hibernate-commons-annotations-3.0.0.ga,hibernate-entitymanager-3.2.1.ga並且讓我知道。 –

+0

okey我會盡力謝謝你:) – AchrafElafrit