Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: How to set a column as unique
PostPosted: Mon Aug 29, 2005 6:52 pm 
Newbie

Joined: Tue Sep 21, 2004 6:10 pm
Posts: 16
Location: Toronto, Canada
Hi there,

I am trying to make a column unique using @Column(unique=true) but it is not working at all. I am still able to insert several rows with the same value in the column I am trying to make unique.
Is there any other way to do it??
I am using derby as DB.

thanks

Erick.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 30, 2005 1:41 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7179
Location: Atlanta, USA
I do have unit tests for that. So check whether it's a bug in derby or not. But wo any showing code, I can't tell.

_________________
Emmanuel
Check Hibernate Search in Action out


Top
 Profile  
 
 Post subject: @Column(unique=true)
PostPosted: Tue Aug 30, 2005 8:55 pm 
Newbie

Joined: Tue Sep 21, 2004 6:10 pm
Posts: 16
Location: Toronto, Canada
Hi Emmanuell,
Thanks for your reply.
I have a class that serves as base for a whole bunch of classes in my object model. This class is the one that have the field I need to make static.

Here is the class:

Code:
@EmbeddableSuperclass
public class NamedPersistentSearchKey extends PersistentSearchKey implements
      NamedEntity {

   /**
    * This method checks if the parameter is of type <code>NamedPersistentSearchKey<code>. If it is,
    * it returns true if the instances have the same Id <b>OR</b> if both instances have <br>
    * the same <code>name</code>.
    * This method is defined 
    * 
    * @see com.thewoodexplorer.model.DbEntityImpl#equals(java.lang.Object)
    */
   @Override
   public boolean equals(Object obj) {
      if (obj.getClass().equals(this.getClass())){
         return this == obj || super.equals(obj) || ((NamedPersistentSearchKey) obj).getName().equals(getName());
      }
      return false;
   }

   /**
    *
    */
   public NamedPersistentSearchKey() {
      super();
   }

   /**
    * @param theName
    */
   public NamedPersistentSearchKey(String theName) {
      super(theName);
   }
   
   /**
    *
    * @param theId
    */
   public NamedPersistentSearchKey(int theId) {
      super(theId);
   }

   /* (non-Javadoc)
    * @see com.thewoodexplorer.model.NamedEntity#getName()
    */
   @Column(length=2048, unique=true)
   public String getName() {
      if (getValue()!=null)
         return getValue().toString();
      else
         return "";
   }

   /* (non-Javadoc)
    * @see com.thewoodexplorer.model.NamedEntity#setName(java.lang.String)
    */
   public void setName(String theName) {
      setValue(theName);
   }

   /* (non-Javadoc)
    * @see java.lang.Object#toString()
    */
   @Override
   @Transient
   public String toString() {
      if (getValue()==null)
         return "";
      else
         return getValue().toString();
   }

}


The sql that hibernate is generating for the creation of tables for classes that extend the class above does not have the unique modifier in it.
Here is a sample:

2005-08-30 13:54:25,437 DEBUG [org.hibernate.tool.hbm2ddl.SchemaExport] - <create table Staining (
id integer not null,
name varchar(2048),
searchSection integer,
primary key (id)
)>

And here is the code for this particular class.

Code:
@Entity(access = AccessType.PROPERTY)
@org.hibernate.annotations.Proxy(lazy=false)
@Table()
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public class Staining extends NamedPersistentSearchKey {

   /**
    *
    */
   public Staining() {
      super();
      // TODO Auto-generated constructor stub
   }

   /**
    * @param theId
    */
   public Staining(int theId) {
      super(theId);
      // TODO Auto-generated constructor stub
   }

}



Thanks a lot..

edovale


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 01, 2005 7:01 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7179
Location: Atlanta, USA
Works for me.

_________________
Emmanuel
Check Hibernate Search in Action out


Top
 Profile  
 
 Post subject: @Column(unique)
PostPosted: Fri Sep 02, 2005 11:43 am 
Newbie

Joined: Tue Sep 21, 2004 6:10 pm
Posts: 16
Location: Toronto, Canada
Hi Emmanuel,
It still does not work for me. I am using the latest releases of everything (hibernate-3.1beta1 and hibernate-annotations-3.1beta4)
Do you think it could be related to the Derby dialect?? Does it make any sense to test against other DB?

Thanks..
edovale


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 03, 2005 8:23 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7179
Location: Atlanta, USA
OK,
I've tested Hibernate Annotations on Derby.
This database has too many limitations: I won't support the test suit on it, and I suggest you to switch to another one if you want to use hibernate (annotation) DDL generation.
For your particular problem, Derby does not allow a unique column to be nullable, so you'll have to make it not null. This also means they don't support the notion of optional one-to-one relationship through a FK column (at least constrained at the DB level).

An other thing. Embedded Derby seams to be unusable when you create/drop hundreads of tables. This getting slower and slower. Even wo that, creating a table is damn slow compared to other DB I've seen.

_________________
Emmanuel
Check Hibernate Search in Action out


Top
 Profile  
 
 Post subject: @Column(unique)
PostPosted: Sat Sep 03, 2005 9:55 am 
Newbie

Joined: Tue Sep 21, 2004 6:10 pm
Posts: 16
Location: Toronto, Canada
Thanks Emmanuel,
I did notice this slowness you mention. My DB have around 80 tables and it certainly is slow while exporting the schema.
I need to use an embedded DB. I nkow that mySql can be embedded.. Do you know of any other with the smallest foot print possible???

Thanks a lot.

Erick.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 03, 2005 11:04 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7179
Location: Atlanta, USA
HSQLDB is a small footprint Java database. We use it for the hibernate unit tests

_________________
Emmanuel
Check Hibernate Search in Action out


Top
 Profile  
 
 Post subject: Derby
PostPosted: Fri Sep 30, 2005 7:34 pm 
Newbie

Joined: Fri Sep 30, 2005 6:51 pm
Posts: 1
Location: San Diego
emmanuel wrote:
OK,
I've tested Hibernate Annotations on Derby.
This database has too many limitations: I won't support the test suit on it, and I suggest you to switch to another one if you want to use hibernate (annotation) DDL generation.
For your particular problem, Derby does not allow a unique column to be nullable, so you'll have to make it not null. This also means they don't support the notion of optional one-to-one relationship through a FK column (at least constrained at the DB level).

An other thing. Embedded Derby seams to be unusable when you create/drop hundreads of tables. This getting slower and slower. Even wo that, creating a table is damn slow compared to other DB I've seen.


Many Derby users report success with Hibernate, so I'm planning to dive in and understand what works well and what doesn't.

Derby 10.1 adds a relaxed durability option that speeds performance at the expense of durability -- it's intended for testing and development, so might be a good option for unit tests. Anyone who would like more info can find it at http://db.apache.org/derby/docs/10.1/tu ... ility.html . Of course, that option should be turned off for production use, but typically we don't find end users creating hundreds of tables in the normal run of a production application.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.