Unique constraints on a combination of columns

5 posts / 0 new
Last post
gayathri
Offline
Joined: 01/12/2010
Unique constraints on a combination of columns

Hi Vincent,

 

     I was wondering if we can define unicity scope on multiple columns. For example, lets say there is a table A with three columns C1, C2, C3, C4 and the requirement states the combination of C2 and C3 is unique. I defined unicity scope for C2 and C3 in the model.xml. When I start the application and I enter the same values for C2 multiple times but enter different values for C3, it gives me the message the Hibernate is unable to perform JDBC batch statement. Please let me know if I am doing something wrong. Also I was wondering if we can customise the message that is displayed when the unique constraint is violated.

Thanks for your help.

 

Thanks

Gayathri

maxime
Offline
Joined: 06/23/2008
Unique constraints on a combination of columns

Hi Gayathri

Did you seth the same property value for both C2 and C3 ?

For example :

  <bean
      id="C"
      class="org.jspresso.framework.model.descriptor.entity.basic.BasicEntityDescriptor">
      <property
         name="propertyDescriptors">
         <list>
            <bean
               class="org.jspresso.framework.model.descriptor.basic.BasicStringPropertyDescriptor">
               <property
                  name="name"
                  value="c2" />
               <property
                  name="unicityScope"
                  value="c2-c3" />
            </bean>
            <bean
               class="org.jspresso.framework.model.descriptor.basic.BasicStringPropertyDescriptor">
               <property
                  name="name"
                  value="c3" />
               <property
                  name="unicityScope"
                  value="c2-c3" />
            </bean>
...

I'm using this feature and it works well :o) Also, for more complexe business rule, i'm using 'GATES' (see other posts on this forum).

Regards,

Maxime

 

gayathri
Offline
Joined: 01/12/2010
Unique constraints on a combination of columns

Hi Maxim,

 

   Thanks for your response.

    Yes, That was it. I had not given the same name. It works.Thanks.

    I was wondering if there was a way to customise the message that is being displayed. Right now, it displays Hibernate exception.

Thanks again for your help.

Thanks

Gayathri

vvandens
Offline
Joined: 05/29/2008
Unique constraints on a combination of columns

Hi Gayathri,

You can customize the way exception are handled by registering a custom exception handler in the controller. see the documentation here. Refining DB exception is sometimes a tricky job. It depends on the DB, the JDBC driver, ...

Also note that although we have explicitely named the unicity contraint during the generation, the Hibernate hbm2ddl schema generation tool just ignores the given constraint name and generates exotic namings. It does not make our life easier ;-) take a look at this post.

HSQL is known not to correctly report SQL error codes during batch update if a failure occurs in one of the batched SQL. This prevents Jspresso to cleanly handle these exceptions. HSQL will be fixed in the next 1.9 release. Until then, the workaround is to disable jdbc batching when using HSQL by completing the context.xml files of existing projects, adding in the hibernateSessionFactory definition :

   <prop key="hibernate.jdbc.batch_size">0</prop>

 

HTH,

Vincent

gayathri
Offline
Joined: 01/12/2010
Unique constraints on a combination of columns

Hi Vincent,

 

  Thanks for your response. I will try the suggestions that you have suggested.

Thanks

Gayathri