-->
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.  [ 3 posts ] 
Author Message
 Post subject: Bug in @UniqueConstraint(columnNames ...) ?
PostPosted: Wed May 07, 2008 12:02 pm 
Beginner
Beginner

Joined: Thu Nov 16, 2006 11:34 am
Posts: 26
Location: Boston
Hibernate version: 3.2.6.GA

Name and version of the database you are using: Oracle 10g

Using JPA annotations in Seam with schema generation (hbm-ddl) and a custom naming Strategy.

Code:
public class MyNamingStrategy extends ImprovedNamingStrategy {
   
    @Override
    public String foreignKeyColumnName(String propertyName, String propertyEntityName,
                    String propertyTableName, String referencedColumnName) {
        return columnName(propertyName) + "_id";
    }

    @Override
    public String classToTableName(String className) {
        return pluralize(super.classToTableName(className));
    }

    private String pluralize(String name) {
        StringBuilder p = new StringBuilder(name);
        if (name.endsWith("y")) {
            p.deleteCharAt(p.length() - 1);
            p.append("ies");
        } else {
            p.append('s');
        }
        return p.toString();
    }
}


Here is my entity

Code:
@Entity
@Table(   uniqueConstraints={@UniqueConstraint(columnNames={"bar_id", "time_period"})})         
public class Foo extends BaseEntity {
   Fof fof;
        private Date timePeriod;
        // others

       @ManyToOne
       @JoinColumn
   public Fof getFof() {
      return fof;
   }

       public Date getTimePeriod() {
      return timePeriod;
   }

        //others

}

I get an exception,

Code:
org.hibernate.AnnotationException: Unable to create unique key constraint (bar_id, time_period) on table foos: time_period not found
   at org.hibernate.cfg.AnnotationConfiguration.buildUniqueKeyFromColumnNames(AnnotationConfiguration.java:604)
   at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:343)
   at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1121)
   at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1225)
   at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:159)


This is really strange, because, the generated table DOES have time_period column. However if I change it to
@Table( uniqueConstraints={@UniqueConstraint(columnNames={"bar_id", "timePeriod"})})

This is incorrect usage but surprisingly working. How can the mapping mix and match the column names and entity property names in one annotation?

Something is seriously wrong in here.

Note that, the entire mapping is perfectly fine as it's a working code for the past three months. We've had no problems with CRUD operations or queries and the generated DDL.

_________________
www.reverttoconsole.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 07, 2008 1:27 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

I think the behavior might be correct. This comes from the annotation configuration:

Quote:
Note that the columnNames array refers to the logical column names.
The logical column name is defined by the Hibernate NamingStrategy implementation. The default EJB3 naming strategy use the physical column name as the logical column name. Note that this may be different than the property name (if the column name is explicit). Unless you override the NamingStrategy, you shouldn't worry about that.


http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#d0e233.

Since you implemented your own naming strategy you end up with this 'issue'.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 07, 2008 1:35 pm 
Beginner
Beginner

Joined: Thu Nov 16, 2006 11:34 am
Posts: 26
Location: Boston
Interesting.

Does that imply that overriding NamingStrategy implies a mixed up column names?

Doesn't sound correct to me, but anyways It works fine for now; got better problems to solve

_________________
www.reverttoconsole.com


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