我有一个非常简单的Hibernate设置,但我收到这个奇怪的错误时,保存一个实体:
org.hibernate.property.access.spi.属性访问字段[私有java.lang.字符串com.example.demo.Student.first名称]通过反射为持久属性[com.example.demo.学生#firstName]:学生[id=0, firstName=My, lastName=Name]
这就是我保存它的方式:
StudentDao studentDao = new StudentDao();
Student student = new Student("My", "Name");
studentDao.saveStudent(student);
这是我的学生实体:
package com.example.demo;
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 = "student")
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "first_name")
private String firstName;
@Column(name = "last_name")
private String lastName;
public Student() {
}
public Student(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "Student [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
学生道:
package com.example.demo;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.example.demo.Student;
import com.example.demo.HibernateUtil;
public class StudentDao {
public void saveStudent(Student student) {
Transaction transaction = null;
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
// start a transaction
transaction = session.beginTransaction();
// save the student object
session.save(student);
// commit transaction
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
if (transaction != null) {
transaction.rollback();
}
}
}
public List <Student> getStudents() {
try (Session session = HibernateUtil.getSessionFactory().openSession()) {
return session.createQuery("from Student", Student.class).list();
}
}
}
HibernateUtil:
package com.example.demo;
import java.util.Properties;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry;
import com.example.demo.Student;
public class HibernateUtil {
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
Configuration configuration = new Configuration();
// Hibernate settings equivalent to hibernate.cfg.xml's properties
Properties settings = new Properties();
settings.put(Environment.DRIVER, "com.mysql.cj.jdbc.Driver");
settings.put(Environment.URL, "jdbc:mysql://localhost:3306/hibernate_db?useSSL=false&serverTimezone=UTC");
settings.put(Environment.USER, "root");
settings.put(Environment.PASS, "root");
settings.put(Environment.DIALECT, "org.hibernate.dialect.MySQL5Dialect");
settings.put(Environment.SHOW_SQL, "true");
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
settings.put(Environment.HBM2DDL_AUTO, "create-drop");
configuration.setProperties(settings);
configuration.addAnnotatedClass(Student.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
e.printStackTrace();
}
}
return sessionFactory;
}
}
还有我的依赖:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.3.7.Final</version>
</dependency>
我尝试了不同版本的hibernate core,但没有任何效果。
我使用的是spring框架。在删除以下依赖项后,它起到了作用:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
/实体类/包实体的代码; 完整堆栈跟踪是
当我试图获得实体属性时,我遇到了奇怪的问题: 我的实体:
用Mysql,intellij作为IDE在Spring启动时使用JPA。 当我尝试保存游戏实体时,我会遇到以下错误,但如果保存玩家实体,则所有操作都很正常 谢谢
问题内容: 我找到了一种通过来获取继承成员并通过来获得 ; 私有成员的方法,但是我正在寻找私有的继承字段。我怎样才能做到这一点? 问题答案: 这应该演示如何解决它: (或Class.getDeclaredFields用于所有字段的数组。) 输出:
问题内容: 考虑这个例子: 允许您通过反射来访问类的私有字段似乎是不合逻辑的。为什么有这样的功能?允许这样的访问不是“危险”吗? 问题答案: 专用旨在防止意外滥用,而不是作为一种安全机制。如果您选择绕过它,那么您可以自行承担风险,并假设您知道自己在做什么。
问题内容: Java中是否可以通过反射访问私有字段str?例如获取该字段的值。 问题答案: 是。 然后,使用字段对象获取类实例上的值。 请注意,方法通常会使人们感到困惑。你有该字段,但没有该对象的实例。你必须将其传递给方法