2016-10-13 7 views
1

私は休止状態の初心者です。私はテーブルにデータを挿入しようとしています。それはテーブルを作成するだけですが、データをテーブルに挿入することはありません。私はどこが間違っているのか分かりません。休止状態、データがテーブルに挿入されていません

助けがあれば助かります。

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> 

コンソールエラー:

@GeneratedValue(strategy = GenerationType.IDENTITY) 

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 : [email protected] 
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.[email protected]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') 

答えて

1

変更

@GeneratedValue(strategy = GenerationType.AUTO) 

私はわからないが、それは私が考えていきます。

編集

AUTO Indicates that the persistence provider should pick an appropriate strategy for the particular database.

IDENTITY Indicates that the persistence provider must assign primary keys for the entity using database identity column.

SEQUENCE Indicates that the persistence provider must assign primary keys for the entity using database sequence column.

TABLE Indicates that the persistence provider must assign primary keys for the entity using an underlying database table to ensure uniqueness. Refer to the API here

http://docs.oracle.com/javaee/5/api/javax/persistence/GenerationType.html

+0

@Parth Solanki、それは働きました。今すぐデータがテーブルに挿入されています。しかし、それがなぜAUTOで働いていなかったのですか? AUTOとIDENTITYの違いは何ですか? – rcr

+0

私はあなたの答えを説明する必要があります。 –

+0

@ ParthSolanki、申し訳ありませんupvoteしようとしましたが、少なくとも15ポイントを持っている必要があると言います。一度私が15ポイントを得たら、私はあなたに投票します。ありがとう。 – rcr