gpt4 book ai didi

Hibernate buildSessionFactory() 异常

转载 作者:行者123 更新时间:2023-12-03 06:42:52 25 4
gpt4 key购买 nike

我在 hibernate 方面遇到了严重的问题。我遵循了各种书籍和在线教程,但我得到了同样的异常“ExceptionInInitializerError”,显然是由 HibernateUtil.java 行抛出的

SessionFactory sf = cfg.configure().buildSessionFactory();

我的 Tomcat 日志显示以下内容:

Caused by: java.lang.ExceptionInInitializerError
at de.marcelstuht.nerven2.server.HibernateUtil.buildSessionFactory(HibernateUtil.java:23)
at de.marcelstuht.nerven2.server.HibernateUtil.<clinit>(HibernateUtil.java:8)
at de.marcelstuht.nerven2.shared.model.Account.<init>(Account.java:52)
at de.marcelstuht.nerven2.server.GreetingServiceImpl.loginServer(GreetingServiceImpl.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:569)
... 21 more

Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:180)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:131)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:345)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
at de.marcelstuht.nerven2.server.HibernateUtil.buildSessionFactory(HibernateUtil.java:18)
... 29 more

Caused by: org.hibernate.InstantiationException: could not instantiate test objectde.marcelstuht.nerven2.shared.model.Account
at org.hibernate.engine.internal.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:49)
at org.hibernate.engine.internal.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:68)
at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:75)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:143)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:498)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:142)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:158)
... 34 more

Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.hibernate.engine.internal.UnsavedValueFactory.instantiate(UnsavedValueFactory.java:46)
... 44 more

Caused by: java.lang.NullPointerException
at de.marcelstuht.nerven2.shared.model.Account.<init>(Account.java:52)
... 49 more

帐户.java

package de.marcelstuht.nerven2.shared.model;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.Session;
import org.hibernate.criterion.*;

import de.marcelstuht.nerven2.server.HibernateUtil;

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

private static final long serialVersionUID = 2675108132819989138L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
Long id;

@NotNull
@Column(name = "username", unique = true)
String username;

@Column(name = "password")
String password;

@Column(name = "email")
String email;

protected Session session;


public Account() {
session = HibernateUtil.getSessionFactory().openSession();
}

public Account(Long id) {
this.id = id;
session = HibernateUtil.getSessionFactory().openSession();
}

public Account(Long id, String username, String password, String email) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
session = HibernateUtil.getSessionFactory().openSession();

}

public void setId(Long newid) {
id = newid;
}

public Long getId() {
return id;
}

public void setUsername(String user) {
username = user;
}

public String getUsername() {
return username;
}

public void setEmail(String mail) {
email = mail;
}

public String getEmail() {
return email;
}

public void setPassword(String newPass) {
password = newPass;
}

public void setNewPassword(String newPass) {
password = hashPassword(newPass);
}

public boolean checkPassword(String pass) {
pass = hashPassword(pass);
if (password.equals(pass)) return true;
else return false;
}

private String hashPassword(String password) {
return DigestUtils.shaHex("wudu390909dfh"+password);
}

public Account get(Long id) {
session.beginTransaction();
Account result = (Account) session.get("account", id);
session.getTransaction().commit();
return result;
}

public Long saveOrUpdate(Account account) {
if (account == null) {
return null;
}

session.beginTransaction();

session.saveOrUpdate(account);
Long id = (Long) session.getIdentifier(account);

session.getTransaction().commit();
return id;
}

public void delete(Account account) {
if (account == null) {
return;
}
session.beginTransaction();
session.delete(account);
session.getTransaction().commit();
}

public List<Account> getAccountByUsername(String user) {
session.beginTransaction();

@SuppressWarnings("unchecked")
List<Account> result = session
.createCriteria(Account.class)
.add(Restrictions.eq("username", user))
.list();
session.getTransaction().commit();

return result;
}

}

HibernateUtil.java

package de.marcelstuht.nerven2.server;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.jboss.logging.Logger;

public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();

protected HibernateUtil() {

}

private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
return sf;
} catch (Exception ex) {
// Make sure you log the exception, as it might be swallowed
Logger.getLogger(HibernateUtil.class).error("Initial SessionFactory creation failed.", ex);
throw new ExceptionInInitializerError(ex);
}
}

public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

hibernate.cfg.xml

<!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>

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/ba</property>
<property name="connection.username">*myuser*</property>
<property name="connection.password">*mypassword*</property>

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

<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>

<!-- Disable the second-level cache -->
<!-- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>-->

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.bytecode.provider">javassist</property>

<!-- Names the annotated entity class -->
<mapping class="de.marcelstuht.nerven2.shared.model.Account"/>

</session-factory>

我尝试了 Hibernate 3.6.9 和 4.0.1(在不同的 Eclipse 项目中)。Skitch 上提供了我添加到项目中的 JAR 的屏幕截图:https://skitch.com/marcelstuht/g7h8h/properties-for-nerven2

最佳答案

引发者:java.lang.NullPointerException在 de.marcelstuht.nerven2.shared.model.Account.(Account.java:52)... 49 更多

从 Account 的构造函数中删除任何内容。你有一个循环依赖。 Hibernate 使用公共(public)的无参数构造函数来处理它的 beans

  1. HibernateUtil 加载
  2. 这会强制静态运行 buildSessionFactory
  3. Hibernate 通过无参数实例化 Account
  4. 这又调用 HibernateUtil......

相信我:除了 POJO setter 之外,没有任何代码的公共(public)无参数构造函数。没有别的了。

关于Hibernate buildSessionFactory() 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9104221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com