52

我实际上是休眠新手,并尝试设置 2 个类。帐户和人员。现在我要做的就是使用注释创建一对一的双向依赖关系。以下是一些细节和代码片段:

这是我添加到构建路径中的库,也许我错过了任何东西:

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;
    }
}

休眠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>

这是我的问题。当我尝试启动一个简单的主类创建一些对象并保存它们时,只是为了测试,我得到了这个:

(%%%% Error Creating SessionFactory %%%% = 我在 printStackTrace 之前的 try-catch 的结果)

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 运行。

谢谢。

4

7 回答 7

82

我从 jdk10 升级到 jdk11 时遇到了这个错误。添加以下依赖项解决了该问题:

<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.25.0-GA</version>
</dependency>
于 2019-05-14T08:14:38.067 回答
49

salt如异常所示,您缺少属性的设置器

请将设置器添加为

 public void setSalt(long salt) {
      this.salt=salt;
 }
于 2013-08-04T11:10:03.953 回答
20

如果您查看异常链,问题是

引起:org.hibernate.PropertyNotFoundException:在类 backend.Account 中找不到属性盐的设置器

问题是方法 Account.setSalt() 在您创建实例时工作正常,但在您从数据库中检索实例时却不行。这是因为您不想在每次加载帐户时都创建新的盐。

要解决此问题,请创建一个可见性为私有的方法 setSalt(long),Hibernate 将能够设置该值(只是注意,我认为它适用于 Private,但您可能需要将其打包或保护)。

于 2013-08-04T11:11:26.117 回答
1

我通过从 springfox 中排除 byte-buddy 依赖解决了这个问题

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.7.0</version>
  <exclusions>
  <exclusion>
    <groupId>net.bytebuddy</groupId>
    <artifactId>byte-buddy</artifactId>
  </exclusion>
  </exclusions>
</dependency>

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.7.0</version>
  <exclusions>
  <exclusion>
    <groupId>net.bytebuddy</groupId>
    <artifactId>byte-buddy</artifactId>
  </exclusion>
</exclusions>
</dependency>
于 2020-05-04T18:25:21.277 回答
1

检查以下情况

  • 1 你的实体类有默认构造函数
  • 2检查所有的getter和setter方法和字符串(自动生成)
  • 3 添加依赖https://mvnrepository.com/artifact/org.javassist/javassist
  • 4 重启你的服务器
  • 5 尝试为数据库连接设置错误的通行证并重新启动服务器,然后再次输入正确的通行证并重新启动(不确定但对我有用)
于 2021-03-19T21:21:12.707 回答
0

在我的情况下,问题只能通过实现如下可序列化来解决:

@Entity @Table(name = "User" , uniqueConstraints = { @UniqueConstraint(columnNames = {"nam"}) }) public class User extends GenericT implements Serializable

于 2020-04-27T10:57:50.657 回答
0

就我而言,问题是因为罐子冲突。

这是对我来说绝对没问题的罐子的完整列表。

antlr-2.7.7.jar
byte-buddy-1.8.12.jar
c3p0-0.9.5.2.jar
classmate-1.3.4.jar
dom4j-1.6.1.jar
geolatte-geom-1.3.0.jar
hibernate-c3p0-5.3.1.Final.jar
hibernate-commons-annotations-5.0.3.Final.jar
hibernate-core-5.3.1.Final.jar
hibernate-envers-5.3.1.Final.jar
hibernate-jpamodelgen-5.3.1.Final.jar
hibernate-osgi-5.3.1.Final.jar
hibernate-proxool-5.3.1.Final.jar
hibernate-spatial-5.3.1.Final.jar
jandex-2.0.3.Final.jar
javassist-3.22.0-GA.jar
javax.interceptor-api-1.2.jar
javax.persistence-api-2.2.jar
jboss-logging-3.3.2.Final.jar
jboss-transaction-api_1.2_spec-1.1.1.Final.jar
jts-core-1.14.0.jar
mchange-commons-java-0.2.11.jar
mysql-connector-java-5.1.21.jar
org.osgi.compendium-4.3.1.jar
org.osgi.core-4.3.1.jar
postgresql-42.2.2.jar
proxool-0.8.3.jar
slf4j-api-1.6.1.jar
于 2018-09-05T15:27:12.383 回答