我正在编辑我的原始问题,因为我改变了很多,但我仍有同样的问题:
所以我的Model类是:
package beans;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "person")
@Access(AccessType.PROPERTY)
public class Person implements Serializable {
private IntegerProperty id;
private StringProperty firstName;
private StringProperty lastName;
private IntegerProperty age;
private StringProperty address;
public Person() {
}
public Person(int id, String firstName, String lastName, int age, String address) {
this.id = new SimpleIntegerProperty(id);
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
this.age = new SimpleIntegerProperty(age);
this.address = new SimpleStringProperty(address);
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
public int getId() {
return id.get();
}
public void setId(int id) {
this.id = new SimpleIntegerProperty(id);
}
public IntegerProperty IdProperty() {
return id;
}
@Column(name = "firstname")
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String firstName) {
this.firstName = new SimpleStringProperty(firstName);
}
public StringProperty FirstNameProperty() {
return firstName;
}
@Column(name = "lastname")
public String getLastName() {
return lastName.get();
}
public void setLastName(String lastName) {
this.lastName = new SimpleStringProperty(lastName);
}
public StringProperty LastNameProperty() {
return lastName;
}
@Column(name = "age")
public int getAge() {
return age.get();
}
public void setAge(int age) {
this.age = new SimpleIntegerProperty(age);
}
public IntegerProperty AgeProperty() {
return age;
}
@Column(name = "address")
public String getAddress() {
return address.get();
}
public void setAddress(String address) {
this.address = new SimpleStringProperty(address);
}
public StringProperty AddressProperty() {
return address;
}
@Override
public String toString() {
return "Person{" + "id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + ", address=" + address + '}';
}
}
我的保存方法是:
@FXML
private void savePerson() {
Person p = new Person();
p.setFirstName(firstnameF.getText());
p.setLastName(lastnameF.getText());
p.setAge(Integer.parseInt(ageF.getText()));
p.setAddress(addressF.getText());
System.out.println(p);
Configuration cfg = new Configuration().configure("hibernate.cfg.xml");
SessionFactory sf = cfg.buildSessionFactory();
try (Session session = sf.openSession()) {
session.beginTransaction();
session.save(p);
session.getTransaction().commit();
clearFields();
} catch (Exception ex) {
ex.printStackTrace();
}
}
我得到的最终错误是:
Person{id=IntegerProperty [value: 1], firstName=StringProperty [value: a], lastName=StringProperty [value: at], age=IntegerProperty [value: 2], address=StringProperty [value: a]}
Jan 25, 2017 7:59:11 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.7.Final}
Jan 25, 2017 7:59:11 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jan 25, 2017 7:59:11 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Jan 25, 2017 7:59:11 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Jan 25, 2017 7:59:11 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/JavafxHibernateTest]
Jan 25, 2017 7:59:11 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=root, password=****}
Jan 25, 2017 7:59:11 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Jan 25, 2017 7:59:11 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 50 (min=1)
Wed Jan 25 19:59:12 CET 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Jan 25, 2017 7:59:12 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Jan 25, 2017 7:59:12 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop
INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/JavafxHibernateTest]
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8413)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:381)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:417)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 48 more
Caused by: org.hibernate.MappingException: Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:123)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:77)
at org.hibernate.metamodel.internal.MetamodelImpl.initialize(MetamodelImpl.java:128)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:297)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:445)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:710)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:726)
at beans.UiController.savePerson(UiController.java:43)
... 58 more
Caused by: org.hibernate.PropertyAccessException: Exception occurred inside getter of beans.Person.id
at org.hibernate.property.access.spi.GetterMethodImpl.get(GetterMethodImpl.java:44)
at org.hibernate.engine.internal.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:68)
at org.hibernate.tuple.PropertyFactory.buildIdentifierAttribute(PropertyFactory.java:61)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:135)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:519)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:124)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:96)
... 65 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.hibernate.property.access.spi.GetterMethodImpl.get(GetterMethodImpl.java:41)
... 75 more
Caused by: java.lang.NullPointerException
at beans.Person.getId(Person.java:42)
... 80 more
BUILD SUCCESSFUL (total time: 31 seconds)
我真的不确定似乎是什么问题,我的模型工作正常,如果它的原始类型而不是属性类型,所以我假设模型很好,我改变了注释系统和问题仍然存在,我清理了我的数据库,但我总是得到一个错误。
答案 0 :(得分:2)
为您的班级添加@Access(AccessType.PROPERTY)
注释。现在Hibernate将在getter方法中查找列。然后将@Column(name = "id")
注释添加到您的getter。
@Entity
@Table(name = "person")
@Access(AccessType.PROPERTY)
public class Person implements Serializable {
private IntegerProperty id;
private StringProperty firstName;
private StringProperty lastName;
private IntegerProperty age;
private StringProperty address;
public Person() {
}
public Person(int id, String firstName, String lastName, int age, String address) {
this.id = new SimpleIntegerProperty(id);
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
this.age = new SimpleIntegerProperty(age);
this.address = new SimpleStringProperty(address);
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
public int getId() {
return id.get();
}
public void setId(int id) {
this.id = new SimpleIntegerProperty(id);
}
public IntegerProperty idProperty() {
return id;
}
@Column(name = "firstname")
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String firstName) {
this.firstName = new SimpleStringProperty(firstName);
}
public StringProperty firstNameProperty() {
return firstName;
}
@Column(name = "lastname")
public String getLastName() {
return lastName.get();
}
public void setLastName(String lastName) {
this.lastName = new SimpleStringProperty(lastName);
}
public StringProperty lastNameProperty() {
return lastName;
}
@Column(name = "age")
public int getAge() {
return age.get();
}
public void setAge(int age) {
this.age = new SimpleIntegerProperty(age);
}
public IntegerProperty ageProperty() {
return age;
}
@Column(name = "address")
public String getAddress() {
return address.get();
}
public void setAddress(String address) {
this.address = new SimpleStringProperty(address);
}
public StringProperty addressProperty() {
return address;
}
}
答案 1 :(得分:0)
我通过chaning删除两个构造函数并使构造函数看起来像这样解决了我的问题:
public Person() {
this.id = new SimpleIntegerProperty(id);
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
this.age = new SimpleIntegerProperty(age);
this.address = new SimpleStringProperty(address);
}
之后一切正常
我还将所有设置者改为
public void setFirstName(String firstName) {
this.firstName = new SimpleStringProperty(firstName);
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
但这与原始问题无关。