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