2

I'm a newbie to hibernate. I'm trying to insert data into table. It's only creating the tables but not inserting the data into table. I'm not sure where I'm doing wrong.

Any help would be appreciable.

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>

Console Error:

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')

1 Answer 1

3

Change

@GeneratedValue(strategy = GenerationType.AUTO)

To

@GeneratedValue(strategy = GenerationType.IDENTITY)

I am not sure , but it will work I think.

Edit

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

Sign up to request clarification or add additional context in comments.

4 Comments

@Parth Solanki, It worked. Now data is inserting into table. But why it was not working with AUTO. What is the difference between AUTO and IDENTITY?
you should explain your answer i think.
@ParthSolanki, Sorry I tried to upvote, But it says I should have at least a 15 points. Once I get 15 points definitely I'll up vote you. Thanks.
@ParthSolanki, that's really helpful. Thanks a lot. By the way I did up vote for you :) (y)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.