-->
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.  [ 6 posts ] 
Author Message
 Post subject: Newbie unique constraint question
PostPosted: Thu Jun 09, 2005 10:27 pm 
Beginner
Beginner

Joined: Thu Jun 09, 2005 2:33 pm
Posts: 30
I have a table in my MySql database that has an auto-incrementing pk. However, I have two other columns that I would like to combine as a unique constraint if possible. I can see how to map one of my pojo properties as unique and make that column unique in the db. But, is it possible to combine say 2 columns under one unique constraint? Thanks alot.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 10:30 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
I need to do this same thing with a table I'm working on, so I'd like to know this one, too. I tried to figure it out from the docs and noticed that a <property> can have multiple columns, so I was going to do something like this:

Code:
<property
   name="column1"
   column="column1"
   not-null="true"
   type="java.lang.Long"
/>

<property
   name="column2"
   column="column2"
   not-null="true"
   type="java.lang.Long"
/>

<property
   name="uniqueconstraint"
   type="java.lang.Long[]"
   unique="true"
   insert="false"
   update="false"
>
   <column name="column1"/>
   <column name="column2"/>
</property>


But Hibernate complains about the type="java.lang.Long[]" (maybe a custom type is necessary??):

Quote:
[hibernatetool] org.hibernate.MappingException: Could not determine type for: java.lang.Integer[], for columns: [org.hibernate.mapping.Column(objid), org.hibernate.mapping.Column(studid)]
[hibernatetool] at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:265)
[hibernatetool] at org.hibernate.tool.hbm2x.Cfg2JavaTool.getRawTypeName(Cfg2JavaTool.java:568)
[hibernatetool] at org.hibernate.tool.hbm2x.Cfg2JavaTool.getJavaTypeName(Cfg2JavaTool.java:470)
[hibernatetool] at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
[hibernatetool] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[hibernatetool] at java.lang.reflect.Method.invoke(Method.java:324)
[hibernatetool] at org.apache.velocity.util.introspection.UberspectImpl$VelMethodImpl.invoke(UberspectImpl.java:267)
[hibernatetool] at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:197)
[hibernatetool] at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:175)
[hibernatetool] at org.apache.velocity.runtime.parser.node.ASTReference.value(ASTReference.java:327)
[hibernatetool] at org.apache.velocity.runtime.parser.node.ASTMethod.execute(ASTMethod.java:128)
[hibernatetool] at org.apache.velocity.runtime.parser.node.ASTReference.execute(ASTReference.java:175)
[hibernatetool] at org.apache.velocity.runtime.parser.node.ASTReference.render(ASTReference.java:220)
[hibernatetool] at org.apache.velocity.runtime.parser.node.ASTBlock.render(ASTBlock.java:55)
[hibernatetool] at org.apache.velocity.runtime.directive.Foreach.render(Foreach.java:166)
[hibernatetool] at org.apache.velocity.runtime.parser.node.ASTDirective.render(ASTDirective.java:114)
[hibernatetool] at org.apache.velocity.runtime.parser.node.ASTBlock.render(ASTBlock.java:55)
[hibernatetool] at org.apache.velocity.runtime.parser.node.ASTIfStatement.render(ASTIfStatement.java:70)
[hibernatetool] at org.apache.velocity.runtime.parser.node.SimpleNode.render(SimpleNode.java:230)
[hibernatetool] at org.apache.velocity.Template.merge(Template.java:256)
[hibernatetool] at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:450)
[hibernatetool] at org.apache.velocity.app.VelocityEngine.mergeTemplate(VelocityEngine.java:419)
[hibernatetool] at org.hibernate.tool.hbm2x.TemplateHelper.processTemplate(TemplateHelper.java:76)
[hibernatetool] at org.hibernate.tool.hbm2x.POJOExporter.runVelocity(POJOExporter.java:91)
[hibernatetool] at org.hibernate.tool.hbm2x.POJOExporter.startPersistentClass(POJOExporter.java:62)
[hibernatetool] at org.hibernate.tool.hbm2x.ConfigurationNavigator.export(ConfigurationNavigator.java:38)
[hibernatetool] at org.hibernate.tool.ant.Hbm2JavaGeneratorTask.execute(Hbm2JavaGeneratorTask.java:42)
[hibernatetool] at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:121)
[hibernatetool] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
[hibernatetool] at org.apache.tools.ant.Task.perform(Task.java:364)
[hibernatetool] at org.apache.tools.ant.Target.execute(Target.java:341)
[hibernatetool] at org.apache.tools.ant.Target.performTasks(Target.java:369)
[hibernatetool] at org.apache.tools.ant.Project.executeTarget(Project.java:1214)
[hibernatetool] at org.apache.tools.ant.Project.executeTargets(Project.java:1062)
[hibernatetool] at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.run(InternalAntRunner.java:403)
[hibernatetool] at org.eclipse.ant.internal.ui.antsupport.InternalAntRunner.main(InternalAntRunner.java:139)
[hibernatetool] WARN getRawTypeName, Could not resolve type without exception for org.hibernate.mapping.Property(constraint1) Value: org.hibernate.mapping.SimpleValue([org.hibernate.mapping.Column(objid), org.hibernate.mapping.Column(studid)]) Type: null. Falling back to typename: java.lang.Integer[]


Is it possible to specify a multi-column unique constraint through the mapping files? And can SchemaExport handle this case?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 10:32 am 
Senior
Senior

Joined: Thu May 12, 2005 11:40 pm
Posts: 125
Location: Canada
<properties>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 10:35 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Super. Don't know how I missed that one. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 11:34 am 
Beginner
Beginner

Joined: Thu Jun 09, 2005 2:33 pm
Posts: 30
Cool. So did that work? Did you have to set up the constraint on the db side as well? I'm assuming yes.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 11:45 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
I haven't tried it yet, but I'm pretty sure SchemaExport will create the constraint for you, if that's what you were asking.


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