-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate mapping relations with Annocations
PostPosted: Fri May 06, 2011 10:52 am 
Newbie

Joined: Fri May 06, 2011 10:42 am
Posts: 1
I'm just stumped on using annotation style hibernate mapping of two objects below. I have a User object, and a Province Table. The primary key of both of these tables are GUID. I cannot save any User object because when I try to do so, what hibernate does is it tries to serialize the "Province" attribute of user i'm saving which becomes over 320 characters and tries to save it for "province_id" column. How do you tell hibernate that I want the "province_id" (the actual primary key of the province) saved ....


the User model

Code:

import java.io.Serializable;
import java.util.Date;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.Email;
import org.hibernate.validator.Length;
import org.hibernate.validator.NotEmpty;
import org.hibernate.validator.Valid;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "USERS",
      uniqueConstraints = {@UniqueConstraint(columnNames={"email"})})
public class User implements Serializable {

   /**
    *
    */
   private static final long serialVersionUID = -4371730534025440938L;

   @Id
   @GeneratedValue(generator = "system-uuid")
   @GenericGenerator(name = "system-uuid", strategy = "uuid")
   @Column(name = "user_id")
   private String id;

   @Column(name = "first_name", nullable = false, length = 20)
   private String firstName;

   @Column(name = "last_name", nullable = false, length = 20)
   private String lastName;

   @Column(name = "email", nullable = false, length = 100)
   private String email;

   @Column(name = "dob_date", nullable = false, columnDefinition = "date")
   private Date dob;

   @Column(name = "home_town", nullable = false, length = 100)
   private String homeTown;

   @Column(name = "province_id", nullable = false, length = 32,
         insertable = true, updatable = true, columnDefinition="varchar", scale = 32)
   private Province province;

   @Column(name = "phone", nullable = false, length = 50)
   private String phone;

   @Column(name = "gender", nullable = false, length = 20)
   private String gender;

   @Column(name = "fb_id", nullable = false, length = 256)
   private String fbId;

   @Column(name = "optin")
   private boolean optin;

   @Column(name = "create_date", updatable = false, columnDefinition = "date")
   private Date createDate;

   public User() {}
   
   public String getId() {
      return id;
   }

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

   @NotEmpty
   @Length(max = 20)
   public String getFirstName() {
      return firstName;
   }

   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }

   @NotEmpty
   @Length(max = 50)
   public String getLastName() {
      return lastName;
   }

   public void setLastName(String lastName) {
      this.lastName = lastName;
   }

   @NotEmpty
   @Email
   public String getEmail() {
      return email;
   }

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

   public Date getDob() {
      return dob;
   }

   public void setDob(Date dob) {
      this.dob = dob;
   }

   @NotEmpty
   public String getHomeTown() {
      return homeTown;
   }

   public void setHomeTown(String homeTown) {
      this.homeTown = homeTown;
   }

   @Valid
   @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   @JoinTable(name = "Provinces",
        joinColumns = @JoinColumn(name="province_id", referencedColumnName="province_id")
    )
   public Province getProvince() {
      return province;
   }

   public void setProvince(Province province) {
      this.province = province;
   }

   @NotEmpty
   public String getPhone() {
      return phone;
   }

   public void setPhone(String phone) {
      this.phone = phone;
   }

   @NotEmpty
   public String getGender() {
      return gender;
   }

   public void setGender(String gender) {
      this.gender = gender;
   }

   @NotEmpty
   public String getFbId() {
      return fbId;
   }

   public void setFbId(String fbId) {
      this.fbId = fbId;
   }

   public boolean isOptin() {
      return optin;
   }

   public void setOptin(boolean optin) {
      this.optin = optin;
   }

   public Date getCreateDate() {
      return createDate;
   }

   public void setCreateDate(Date createDate) {
      this.createDate = createDate;
   }

}




the province model

Code:

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "PROVINCES")
public class Province implements Serializable {

   /**
    *
    */
   private static final long serialVersionUID = 3813930248740187633L;

   public Province() {}
   
   @Id
   @GeneratedValue(generator = "system-uuid")
   @GenericGenerator(name = "system-uuid", strategy = "uuid")
   @Column(name = "province_id")
   private String id;
   
   @Column(name = "CODE")
   private String code;
   
   @Column(name = "NAME")
   private String name;
   
   
   @OneToMany(mappedBy="province", targetEntity = Province.class)
   private transient Set<User> users;
   

   public String getId() {
      return id;
   }

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

   public String getCode() {
      return code;
   }

   public void setCode(String code) {
      this.code = code;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public Set<User> getUsers() {
      return users;
   }

   public void setUsers(Set<User> users) {
      this.users = users;
   }
   
   
   
}



the error

Code:
java.sql.BatchUpdateException: ORA-12899: value too large for column "DBO"."USERS"."PROVINCE_ID" (actual: 320, maximum: 32)

   oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:345)
   oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10844)
   org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
   org.apache.tomcat.dbcp.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
   org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
   org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
   org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
   org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
   org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
   org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
   org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
   org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
   org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
   org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656)
   org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
   org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
   org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
   org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   $Proxy34.addUser(Unknown Source)
   com.coke.web.controllers.UserController.saveArticle(UserController.java:69)
   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   java.lang.reflect.Method.invoke(Unknown Source)
   org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
   org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:426)
   org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414)
   org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
   org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
   org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
   org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
   org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
   org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.11 logs.


Top
 Profile  
 
 Post subject: Re: Hibernate mapping relations with Annocations
PostPosted: Fri May 06, 2011 12:31 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Do not mix annotation on the getter method with annotations on the fields. My guess is that Hibernate is ignoring the @ManyToOne annotation on the getProvince() method since you already have a @Column annotation on the province field.


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

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.