-->
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.  [ 4 posts ] 
Author Message
 Post subject: Unique Constraints referencing superclass property?
PostPosted: Sat Feb 10, 2007 4:33 pm 
Beginner
Beginner

Joined: Thu Oct 16, 2003 7:25 pm
Posts: 38
Location: New York City
I have a pretty fringe case question which the documentation doesn't touch on. I have a number of entities that are using table per class inheritance, and I wish to use a superclasses' property in a subclasses' unique constraint clause. I am using annotations, but I don't believe that the definition for the unique constraints clauses is specific to annotations, but I could be wrong there. Please correct me.

What should the unique constraint on class Attachment, below, be? Can the subclass reference a superclasses' property at all? I've tried "hub.id" and "hubId", but neither works. I suppose I could do without the constraint, but I'm a stickler for my models, and there should be only one attachment with the same uri for each hub. Other Asset subclasses use hub as well, you see.

A quick example, with just the relevant working pieces, follows, as well as the exception I get when generating my schema via the Ant tool task. Thanks for any help!

Tom Harris
thomas.harris@sourcemedia.com
SourceMedia, Inc.

Code:
@Entity
@Table(name = "asset")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Asset implements Serializable {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(nullable = false, updatable = false)
  public long getId() { return _id; }
  public void setId(long arg) { _id = arg; }
  private long _id = 0L;

  @ManyToOne(cascade = {}, fetch = FetchType.EAGER)
  @JoinColumn(name = "hubId", unique = false, nullable = false, insertable = true, updatable = true)
  @LazyToOne(LazyToOneOption.FALSE)
  @Fetch(FetchMode.JOIN)
  public Hub getHub() { return _hub; }
  public void setHub(Hub arg) { _hub = arg; }
  private Hub _hub = null;

  //...
}

@Entity
@Table(name = "attachment", uniqueConstraints = {
  @UniqueConstraint(columnNames = { "hub.id", "uri" }) // <-- HERE
})
public class Attachment extends Asset {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(nullable = false, updatable = false)
  public long getId() { return _id; }
  public void setId(long arg) { _id = arg; }
  private long _id = 0L;

  @Basic
  @Column(length = 100, nullable = false, updatable = true)
  public String getUri() { return _uri; }
  public void setUri(String arg) { _uri = arg; }
  private String _uri = null;

  //...
}


Hibernate version: Hibernate 3.2.2, Annotations and EntityManager 3.2.1.GA

Mapping documents: None.

Full stack trace of any exception that occurs:
Code:
[hibernatetool] org.hibernate.MappingException: Unable to find column with logical name hub.id in table attachme
nt

BUILD FAILED
F:\webpublishing\cms\build.xml:232: org.hibernate.MappingException: Unable to find column with logical name hub.id in table attachment
        at org.hibernate.tool.ant.HibernateToolTask.reportException(HibernateToolTask.java:223)
        at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:186)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
        at org.apache.tools.ant.Task.perform(Task.java:364)
        at org.apache.tools.ant.Target.execute(Target.java:341)
        at org.apache.tools.ant.Target.performTasks(Target.java:369)
        at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
        at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
        at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
        at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
        at org.apache.tools.ant.Main.runBuild(Main.java:668)
        at org.apache.tools.ant.Main.startAnt(Main.java:187)
        at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
        at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)
Caused by: org.hibernate.MappingException: Unable to find column with logical name hub.id in table attachment
        at org.hibernate.cfg.Mappings.getPhysicalColumnName(Mappings.java:493)
        at org.hibernate.cfg.AnnotationConfiguration.buildUniqueKeyFromColumnNames(AnnotationConfiguration.java:488)
        at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:310)
        at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)
        at org.hibernate.tool.ant.ConfigurationTask.getConfiguration(ConfigurationTask.java:56)
        at org.hibernate.tool.ant.HibernateToolTask.getConfiguration(HibernateToolTask.java:299)
        at org.hibernate.tool.ant.Hbm2DDLExporterTask.execute(Hbm2DDLExporterTask.java:45)
        at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:183)
        ... 12 more
--- Nested Exception ---
org.hibernate.MappingException: Unable to find column with logical name hub.id in table attachment
        at org.hibernate.cfg.Mappings.getPhysicalColumnName(Mappings.java:493)
        at org.hibernate.cfg.AnnotationConfiguration.buildUniqueKeyFromColumnNames(AnnotationConfiguration.java:488)
        at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:310)
        at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)
        at org.hibernate.tool.ant.ConfigurationTask.getConfiguration(ConfigurationTask.java:56)
        at org.hibernate.tool.ant.HibernateToolTask.getConfiguration(HibernateToolTask.java:299)
        at org.hibernate.tool.ant.Hbm2DDLExporterTask.execute(Hbm2DDLExporterTask.java:45)
        at org.hibernate.tool.ant.HibernateToolTask.execute(HibernateToolTask.java:183)
        at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
        at org.apache.tools.ant.Task.perform(Task.java:364)
        at org.apache.tools.ant.Target.execute(Target.java:341)
        at org.apache.tools.ant.Target.performTasks(Target.java:369)
        at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
        at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
        at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
        at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
        at org.apache.tools.ant.Main.runBuild(Main.java:668)
        at org.apache.tools.ant.Main.startAnt(Main.java:187)
        at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
        at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)


Name and version of the database you are using: MySQL 5.0


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 01, 2007 1:55 am 
Beginner
Beginner

Joined: Thu Oct 16, 2003 7:25 pm
Posts: 38
Location: New York City
Shall I enter a bug?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 01, 2007 7:19 am 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
I don't think this is a bug, its just not possible. Asset and Attachment are stored in different tables, as you requested. Then, you're asking the DB to create a unique constraint across fields from these tables. As far as I'm aware, unique constraints can only apply to fields of the same table.

btw - I had to remove the @Id from Attachment as it was causing an init error because parent class already defines @id.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 03, 2007 12:17 am 
Beginner
Beginner

Joined: Thu Oct 16, 2003 7:25 pm
Posts: 38
Location: New York City
Yes, you are right on the money. What was I thinking? Most databases will shrug at the notion. Thanks... We have a second layer of defense of the model in a custom spring entity validation engine thing. Whatever, you know what I say. :)

Tom


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