-->
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.  [ 9 posts ] 
Author Message
 Post subject: Problem for use an enum for id
PostPosted: Mon Aug 20, 2007 5:32 am 
Newbie

Joined: Mon Aug 20, 2007 5:14 am
Posts: 9
Hibernate version: 3.2.4 sp1

Name and version of the database you are using: Firebird 1.5.3


Hello,

My problem is how use enum for id :

I have this enum :

Code:
public enum Day {
  monday,
  tuesday,
  wednesday,
  thursday,
  friday,
  saturday,
  sunday
}


and this entity :

@Entity
@Table(name="WEEK")
public class Week {

@Id
@Enumerated(EnumType.STRING)
@Column(length=30)
private Day d;

@Column(length=30)
private String truc;

// + getters and setters
}

if I have in Hibernate configuration
<property name="hibernate.hbm2ddl.auto">create</property>
I get this error :
Quote:
GRAVE: Unsuccessful: create table WEEK (d blob not null, truc varchar(30), primary key (d))


If I create myself the table with this sql code :

Code:
create table WEEK (
d varchar(30) not null,
truc varchar(30));

alter table WEEK add constraint C_WEEK primary key(d);

<property name="hibernate.hbm2ddl.auto">update</property>

and I try to persist an Week entity instance, I get this error :



Quote:
Hibernate: insert into WEEK (truc, d) values (?, ?)
20 août 2007 11:27:46 org.hibernate.type.NullableType nullSafeSet
INFO: could not bind value '2c6d8085fef2809ce5f3f3e1e9aee5eef5ede5f2e1f4e9efeeaef3f4f2e9eee7aec4e1f98080808080808080928080f8f2808eeae1f6e1aeece1eee7aec5eef5ed8080808080808080928080f8f0f48086edefeee4e1f9' to parameter: 2; Data truncation
20 août 2007 11:27:46 org.hibernate.util.JDBCExceptionReporter logExceptions
ATTENTION: SQL Error: 0, SQLState: 01004
20 août 2007 11:27:46 org.hibernate.util.JDBCExceptionReporter logExceptions
GRAVE: Data truncation
20 août 2007 11:27:46 org.hibernate.event.def.AbstractFlushingEventListener performExecutions
GRAVE: Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: could not insert: [essai.enumeration.string.Week]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2267)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:56)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at essai.enumeration.string.WeekDao.create(WeekDao.java:14)
at essai.enumeration.string.Main.main(Main.java:16)
Caused by: java.sql.DataTruncation: Data truncation
at org.firebirdsql.jdbc.field.FBStringField.setBytes(FBStringField.java:341)
at org.firebirdsql.jdbc.AbstractPreparedStatement.setBytes(AbstractPreparedStatement.java:178)
at org.hibernate.type.AbstractBynaryType.set(AbstractBynaryType.java:43)
at org.hibernate.type.SerializableType.set(SerializableType.java:29)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:116)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2008)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2243)
... 12 more
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not insert: [essai.enumeration.string.Week]
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2267)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:56)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at essai.enumeration.string.WeekDao.create(WeekDao.java:14)
at essai.enumeration.string.Main.main(Main.java:16)
Caused by: java.sql.DataTruncation: Data truncation
at org.firebirdsql.jdbc.field.FBStringField.setBytes(FBStringField.java:341)
at org.firebirdsql.jdbc.AbstractPreparedStatement.setBytes(AbstractPreparedStatement.java:178)
at org.hibernate.type.AbstractBynaryType.set(AbstractBynaryType.java:43)
at org.hibernate.type.SerializableType.set(SerializableType.java:29)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:136)
at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:116)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2008)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2243)
... 12 more


It seems that @Enumerated(STRING) is ignored.... I don't understand why.

If anyone have any ideas, thanks you for reply....


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 20, 2007 5:40 am 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

I have no idea why @Enumerated(EnumType.STRING) is not honoured, but as a workaround you could try the following:

Code:
@Id
@Enumerated(EnumType.STRING)
@Column(length=30, columnDefinition="varchar(30)")
private Day d;


Cheers,

Andy

_________________
Rules are only there to be broken


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 20, 2007 5:52 am 
Newbie

Joined: Mon Aug 20, 2007 5:14 am
Posts: 9
Thanks for this idea, but the result isn't very good:

now Hibernate can create the table Week but the column d is varchar(255) instead of varchar(30), and persist a Week entity instance always made the same error.

It's a very strange problem..


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 20, 2007 8:20 am 
Newbie

Joined: Mon Aug 20, 2007 5:14 am
Posts: 9
After search in Sun J2EE doc, I found this doc http://java.sun.com/xml/ns/persistence/orm_1_0.xsd.
It seems that Enumerated couldn't be combined with Id but only with Basic ...

I'm afraid there isn't solution for my problem...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 24, 2007 10:22 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I guess by using a composite id, you could make it work. I haven't tried though

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 16, 2007 4:41 am 
Newbie

Joined: Wed Sep 26, 2007 6:09 am
Posts: 3
I ran into the same problem and tried as Emmanuel suggested. I effectively wrapped the enum field in a primary key class and can now store it using either ORDINAL or STRING by setting the Enumerated annotation on the field within the primary key class.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 01, 2008 12:42 pm 
Beginner
Beginner

Joined: Thu Aug 26, 2004 5:53 am
Posts: 37
I ran into the same problem.

Still working on a solution.

Any chance that hibernate could support this combination ?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 01, 2008 1:28 pm 
Beginner
Beginner

Joined: Thu Aug 26, 2004 5:53 am
Posts: 37
this seems to do the trick.

Code:
   @Id
   @Type(type="org.hibernate.type.EnumType",parameters={
      @Parameter(name="enumClass", value="your.enum.package.CountryCode")
   })   
   @Column(length=2, columnDefinition="char(2)")
   private CountryCode code;
   public CountryCode getCode() {
      return code;
   }
   public void setCode(CountryCode code) {
      this.code = code;
   }


It may be good if hibernate could just support the @Enumerated annotation for this case, since it is really just a shorthand for the above @Type definition


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 02, 2008 1:21 pm 
Beginner
Beginner

Joined: Thu Aug 26, 2004 5:53 am
Posts: 37
Note also that for String enums you need this parameter

Code:
@Parameter(name="type", value="1") // 1=char, 12 = varchar


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.