我是休眠的新手。我正在尝试将数据插入表中。它只创建表但不将数据插入表中。我不确定我在哪里做错了。
任何帮助都会很明显。
的UserDetails:
package org.com.etown.onetoone;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
@Entity
public class UserDetails {
@Id @GeneratedValue(strategy = GenerationType.AUTO)
private int userid;
private String userName;
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
UserDetailsDao:
package org.com.etown.onetoone;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class UserDatailsDao {
public static void main(String[] args) {
UserDetails user = new UserDetails();
user.setUserName("george");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
session.close();
}
}
hibernate.cfg.xml中:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/etowndb</property>
<property name="connection.username">postgres</property>
<property name="connection.password">root</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL Dialect -->
<property name="dialect">org.hibernate.dialect.PostgreSQL92Dialect</property>
<!-- Disable the second level class -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-creat the database schema on startup -->
<property name="hbm2ddl.auto">update</property><!-- update,create -->
<!-- Names of the annotated entity class -->
<mapping class="org.com.etown.onetoone.UserDetails"/>
</session-factory>
</hibernate-configuration>
控制台错误:
Oct 13, 2016 12:46:30 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.1.1.Final}
Oct 13, 2016 12:46:30 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Oct 13, 2016 12:46:30 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Oct 13, 2016 12:46:30 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Oct 13, 2016 12:46:30 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
Oct 13, 2016 12:46:30 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5432/etowndb]
Oct 13, 2016 12:46:30 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=postgres, password=****}
Oct 13, 2016 12:46:30 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Oct 13, 2016 12:46:30 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 1 (min=1)
Oct 13, 2016 12:46:30 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL92Dialect
Oct 13, 2016 12:46:31 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Oct 13, 2016 12:46:31 PM org.hibernate.type.BasicTypeRegistry register
INFO: HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@7ffeac8e
Oct 13, 2016 12:46:31 PM org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl getIsolatedConnection
INFO: HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@31c08b2e] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
Oct 13, 2016 12:46:31 PM org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl processGetTableResults
INFO: HHH000262: Table not found: UserDetails
Oct 13, 2016 12:46:31 PM org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl processGetTableResults
INFO: HHH000262: Table not found: UserDetails
Hibernate: create table UserDetails (userid int4 not null, userName varchar(255), primary key (userid))
Oct 13, 2016 12:46:31 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning
WARN: SQL Warning Code: 0, SQLState: 00000
Oct 13, 2016 12:46:31 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper$StandardWarningHandler logWarning
WARN: CREATE TABLE / PRIMARY KEY will create implicit index "userdetails_pkey" for table "userdetails"
Hibernate: select nextval ('hibernate_sequence')
答案 0 :(得分:3)
更改
@GeneratedValue(strategy = GenerationType.AUTO)
要
@GeneratedValue(strategy = GenerationType.IDENTITY)
我不确定,但我认为它会起作用。
修改强>
AUTO 表示持久性提供程序应该选择 适用于特定数据库的战略。
IDENTITY 表示持久性提供程序必须分配主要 使用数据库标识列的实体的密钥。
SEQUENCE 表示持久性提供程序必须分配主要 使用数据库序列列的实体的键。
TABLE 表示持久性提供程序必须分配主键 对于使用底层数据库表的实体来确保 独特之处。请参阅此处的API
http://docs.oracle.com/javaee/5/api/javax/persistence/GenerationType.html