2013-08-04 99 views
17

我實際上是新的休眠,並試圖設置2類。帳戶和人員。現在我所要做的就是使用註釋創建一對一的雙向依賴關係。以下是一些細節和代碼片段:不能爲org.hibernate.persister.entity.SingleTableEntityPersister構造函數

這裏是我的圖書館,我加入到構建路徑,也許我錯過了什麼:

http://prntscr.com/1jbew4

帳戶:

package backend; 

import java.util.Random; 
import java.util.Collection; 
import java.util.TreeSet; 
import javax.persistence.*; 

@Entity 
@Table(name = "Account") 
public class Account { 

    public static Random rnd = new Random(); 
    private int id; 
    private String username; 
    private long password; 
    private long salt; 
    private Person person; 
    private String role; 
    private Collection<Account> friendlist; 

    public Account() { 

    } 

    public Account(String username, long password, Person person, String role) { 
     super(); 
     this.username = username; 
     this.password = password; 
     this.setSalt(); 
     this.person = person; 
     this.role = role; 
     this.friendlist = new TreeSet<Account>(); 
    } 

    @Id 
    @GeneratedValue 
    @Column(name = "Id") 
    public int getId() { 
     return id; 
    } 

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

    @Column(name = "Username") 
    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    @Column(name = "Password") 
    public long getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = (long) salt + password.hashCode(); 
    } 

    @Column(name = "Salt") 
    public long getSalt() { 
     return salt; 
    } 

    public void setSalt() { 
     this.salt = rnd.nextLong(); 
     if (this.salt < 0) { 
      this.salt = this.salt * (-1); 
     } 
    } 

    @OneToOne(cascade = CascadeType.ALL) 
    @JoinColumn(name = "Person_FK") 
    public Person getPerson() { 
     return person; 
    } 

    public void setPerson(Person person) { 
     this.person = person; 
    } 

    @Column(name = "Role") 
    public String getRole() { 
     return role; 
    } 

    public void setRole(String role) { 
} 

人:

package backend; 

import javax.persistence.*; 

@Entity 
@Table(name ="Person") 
public class Person { 

    private int id; 
    private String firstName; 
    private String lastName; 
    private String street; 
    private int houseNumber; 
    private String tel; 
    private String email; 
    private String mobile; 
    private Account account; 

    public Person() { 

    } 

    public Person(int id, String firstName, String lastName, String street, 
      int houseNumber, String tel, String email, String mobile, 
      Account account) { 
     super(); 
     this.id= id; 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.street = street; 
     this.houseNumber = houseNumber; 
     this.tel = tel; 
     this.email = email; 
     this.mobile = mobile; 
     this.account = account; 
    } 

    @Id 
    @GeneratedValue 
    @Column(name="Id") 
    public int getId() { 
     return id; 
    } 

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

    @Column(name="Firstname") 
    public String getFirstName() { 
     return firstName; 
    } 

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


    @Column(name="Lastname") 
    public String getLastName() { 
     return lastName; 
    } 

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

    @Column(name="Street") 
    public String getStreet() { 
     return street; 
    } 

    public void setStreet(String street) { 
     this.street = street; 
    } 

    @Column(name="Housenumber") 
    public int getHouseNumber() { 
     return houseNumber; 
    } 

    public void setHouseNumber(int houseNumber) { 
     this.houseNumber = houseNumber; 
    } 

    @Column(name="Tel") 
    public String getTel() { 
     return tel; 
    } 

    public void setTel(String tel) { 
     this.tel = tel; 
    } 

    @Column(name="Email") 
    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    @Column(name="Mobile") 
    public String getMobile() { 
     return mobile; 
    } 

    public void setMobile(String mobile) { 
     this.mobile = mobile; 
    } 

    @OneToOne(mappedBy = "person", cascade = CascadeType.ALL) 
    public Account getAccount() { 
     return account; 
    } 

    public void setAccount(Account account) { 
     this.account = account; 
    } 
} 

Hibernate cfg 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> 
     <!-- Alternative 1: MySQL --> 

     <!-- Database connection settings: MySQL --> 
     <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
     <property name="connection.url">jdbc:mysql:///chat.application.db</property> 
     <property name="connection.username">root</property> 
     <property name="connection.password"></property> 
     <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 


     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">1</property> 


     <!-- Enable Hibernate's automatic session context management --> 
     <property name="current_session_context_class">thread</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> 
     <property name="format_sql">true</property> 

     <!-- Drop and re-create the database schema on startup --> 
     <property name="hbm2ddl.auto">create</property> 

     <property name="hibernate.format_sql">true</property> 

     <!-- Auflistung der gemappten Klassen (Package + Class-Name) --> 
     <mapping class="backend.Account"/> 
     <mapping class="backend.Person"/> 

     <!-- <mapping class="backend.Role"/> --> 
    </session-factory> 
</hibernate-configuration> 

這是我的問題。當我嘗試啓動一個簡單的主類創建一些對象並保存它們,只是爲了測試,我得到這個:

(%%%%錯誤創建SessionFactory %%%% =我的try-catch的結果printStackTrace )

INFO: HHH000397: Using ASTQueryTranslatorFactory 
%%%% Error Creating SessionFactory %%%% 
org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister 
    at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:185) 
    at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:135) 
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:385) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1769) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1840) 
    at backend.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:21) 
    at backend.HibernateSessionFactory.getCfg(HibernateSessionFactory.java:48) 
    at backend.Main.main(Main.java:14) 
Caused by: org.hibernate.HibernateException: Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer] 
    at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:138) 
    at org.hibernate.tuple.entity.EntityTuplizerFactory.constructDefaultTuplizer(EntityTuplizerFactory.java:188) 
    at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:341) 
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:507) 
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:146) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:163) 
    ... 7 more 
Caused by: java.lang.reflect.InvocationTargetException 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
    at java.lang.reflect.Constructor.newInstance(Unknown Source) 
    at org.hibernate.tuple.entity.EntityTuplizerFactory.constructTuplizer(EntityTuplizerFactory.java:135) 
    ... 16 more 
Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property salt in class backend.Account 
    at org.hibernate.property.BasicPropertyAccessor.createSetter(BasicPropertyAccessor.java:252) 
    at org.hibernate.property.BasicPropertyAccessor.getSetter(BasicPropertyAccessor.java:245) 
    at org.hibernate.mapping.Property.getSetter(Property.java:326) 
    at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertySetter(PojoEntityTuplizer.java:444) 
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:201) 
    at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:82) 
    ... 21 more 
Exception in thread "main" java.lang.NullPointerException 
    at backend.HibernateSessionFactory.currentSession(HibernateSessionFactory.java:28) 
    at backend.HibernateSessionFactory.getCfg(HibernateSessionFactory.java:48) 
    at backend.Main.main(Main.java:14) 

所以我真的不知道如何處理這個異常。我試圖谷歌它等,但我發現是有些人在他們的代碼中有拼寫錯誤,這是它的原因。但不幸的是我沒有在我的代碼中有拼寫。有人能幫我嗎?如果需要,我也很樂意發佈我的SessionFactoryCode或主類。由於我是冬眠新手,因此我採取每一個建議=)

順便說一句:SQL DB正在通過XAMPP運行。

謝謝。

+1

+1給予足夠的信息,能夠幫助你:) – Augusto

+0

你應該接受寫作答案!顯然,兩個答案都可以解決問題。 –

回答

36

你缺少的salt屬性setter由異常指示

請加上二傳手作爲

public void setSalt(long salt) { 
     this.salt=salt; 
} 
+0

你是對的,缺少鹽的標準制定者。非常感謝 !!!特別是對於響應時間=)你做了我的一天 – Stephen

15

如果你看一下異常鏈,問題是

引起:org.hibernate.PropertyNotFoundException:無法在類後端找到屬性salt的設置程序。帳戶

問題是Account.setSalt()方法在創建實例時工作正常,但在您從數據庫檢索實例時不起作用。這是因爲您每次加載帳戶時都不想創建新的鹽。

要解決這個問題,創建一個方法setSalt(長)與可見性私人和Hibernate將能夠設置值(只是一個筆記,我認爲它適用於私人,但你可能需要使其包裝或保護) 。