-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Using sequence id with Postgresql 9.3
PostPosted: Mon Sep 01, 2014 1:47 pm 
Newbie

Joined: Mon Sep 01, 2014 1:25 pm
Posts: 1
I am learning to use Hibernate with PostgresQL for J2EE projects. I am using Hibernate 4.3.6 and Postgresql 9.3.

My DDL:

Code:
DROP TABLE
IF EXISTS tbl_user;

DROP SEQUENCE
IF EXISTS sq_user;

CREATE TABLE tbl_user (
    ID INTEGER PRIMARY KEY,
    user_name VARCHAR (25) UNIQUE,
    PASSWORD VARCHAR (100) NOT NULL,
    salt VARCHAR (15) NOT NULL,
    full_name VARCHAR (50) NOT NULL,
    email VARCHAR (50) NOT NULL,
    status int2,
    active_code VARCHAR (100)
);

CREATE SEQUENCE sq_user INCREMENT 1 NO MAXVALUE START 1;



My Entity :

Code:
package vn.com.vcb.webdemo.entities;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
* The persistent class for the tbl_user database table.
*
*/
@Entity
@Table(name = "tbl_user")
public class UserEn implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   @Column(name = "id")
   @SequenceGenerator(name = "user_id", sequenceName = "sq_user", allocationSize = 1)
   @GeneratedValue(generator = "user_id", strategy = GenerationType.SEQUENCE)
   private Long id;

   @Column(name = "active_code")
   private String activeCode;
   @Column(name = "email")
   private String email;

   @Column(name = "full_name")
   private String fullName;

   @Column(name = "password")
   private String password;

   @Column(name = "salt")
   private String salt;

   @Column(name = "status")
   private Integer status;

   @Column(name = "user_name")
   private String userName;

   public UserEn() {
   }

   public Long getId() {
      return this.id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public String getActiveCode() {
      return this.activeCode;
   }

   public void setActiveCode(String activeCode) {
      this.activeCode = activeCode;
   }

   public String getEmail() {
      return this.email;
   }

   public void setEmail(String email) {
      this.email = email;
   }

   public String getFullName() {
      return this.fullName;
   }

   public void setFullName(String fullName) {
      this.fullName = fullName;
   }

   public String getPassword() {
      return this.password;
   }

   public void setPassword(String password) {
      this.password = password;
   }

   public String getSalt() {
      return this.salt;
   }

   public void setSalt(String salt) {
      this.salt = salt;
   }

   public Integer getStatus() {
      return this.status;
   }

   public void setStatus(Integer status) {
      this.status = status;
   }

   public String getUserName() {
      return this.userName;
   }

   public void setUserName(String userName) {
      this.userName = userName;
   }

}


My persistence.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
   xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
   <persistence-unit name="webdemo" transaction-type="RESOURCE_LOCAL">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <class>vn.com.vcb.webdemo.entities.UserEn</class>
      <properties>
         <property name="hibernate.connection.username" value="webdemo" />
         <property name="hibernate.connection.password" value="webdemo" />
         <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/webdemo" />
         <property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
         <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect" />
         <property name="hibernate.hbm2ddl.auto" value="update" />
      </properties>
   </persistence-unit>
</persistence>


When I try to insert a new user to the database, it throw an exception like below

Code:

javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: vn.com.vcb.webdemo.entities.UserEn
   at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1763)
   at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677)
   at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683)
   at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1187)
   at vn.com.vcb.webdemo.dao.UserDAO.createUser(UserDAO.java:20)
   at vn.com.vcb.webdemo.bus.UserBus.createUser(UserBus.java:51)
   at vn.com.vcb.webdemo.servlet.RegisterServlet.doPost(RegisterServlet.java:35)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
   at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
   at vn.com.vcb.webdemo.filter.LoginFilter.doFilter(LoginFilter.java:41)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
   at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
   at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
   at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
   at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
   at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
   at java.lang.Thread.run(Thread.java:724)
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: vn.com.vcb.webdemo.entities.UserEn
   at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:139)
   at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
   at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
   at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
   at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
   at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1181)
   ... 28 more


I try many approach found from google but seems they did not work.
Would someone please let me know how to fix it.
Thank you inadvance


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.