I hope that my question is somewhat simple to answer. What I am attempting to do seems simple to me anyway.
I have four tables,
Issue,
Department,
Feature, and
AllIssues. The
Issue table holds the basic information about the issue, date, volume, title story, etc... The
Department and
Feature tables hold an id, an issue id, and the title for the department or feature. The
AllIssues table holds an id and a title. The data that is held in the
AllIssues table is data that is used for every issue, therefore it does not contain an issue id or any foreign key back to the
Issue table.
On the Java side, I have two objects,
Issue and
IssueItem. The
Issue object holds all of the basic info pulled from the
Issue table and also a java.util.Set of
IssueItem objects for the departments, features, and all issue items. What I want to do is have the
IssueItem object mapped to the
Department,
Feature, and
AllIssues tables identified by an entity-name and then map them to the
Issue object. My mapping files are shown below.
The problem that I am having is with the mapping for the
AllIssues table. All I want to do is "select * from AllIssues", create
IssueItem objects from this query and place it in the allIssues Set in the
Issue object. The other mappings seem to be fine but it appears that the problem is because I do not have a foreign key back to the
AllIssues table. How do I get around the foreign key problem? How can I simply populate the set with all of the rows of the table?
I am using this within a Spring Framework context.
Thank you in advance for any help.
Hibernate version:
Hibernate 3.0
Mapping documents:
Issue.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true">
<class name="com.dfischer.primo.data.domain.Issue">
<id name="id" column="id" type="int" unsaved-value="0">
<generator class="increment" />
</id>
<property name="volume" column="volume" />
<property name="issue" column="issue" />
<property name="month" column="month" />
<property name="year" column="year" />
<property name="title" column="title" />
<property name="author" column="author" />
<property name="description" column="description" />
<property name="imagePath" column="imagePath" />
<set name="allIssues"
inverse="false"
lazy="false"
table="allIssues">
<key />
<many-to-many entity-name="everyIssue" />
</set>
<set name="departments"
inverse="true"
cascade="all-delete-orphan"
lazy="false">
<key column="issueId" />
<one-to-many entity-name="department" />
</set>
<set name="features"
inverse="true"
cascade="all-delete-orphan"
lazy="false">
<key column="issueId" />
<one-to-many entity-name="feature" />
</set>
</class>
</hibernate-mapping>
IssueItem.hbm.xmlCode:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping auto-import="true">
<class entity-name="everyIssue"
name="com.dfischer.primo.data.domain.IssueItem"
table="everyIssue">
<id name="id" column="id" type="int" unsaved-value="0">
<generator class="assigned" />
</id>
<property name="description" />
</class>
<class entity-name="department"
name="com.dfischer.primo.data.domain.IssueItem"
table="issueDepartment">
<id name="id" column="id" type="int" unsaved-value="0">
<generator class="increment" />
</id>
<property name="description" />
<many-to-one name="issue"
column="issueId"
class="com.dfischer.primo.data.domain.Issue"
not-null="true"
cascade="save-update" />
</class>
<class entity-name="feature"
name="com.dfischer.primo.data.domain.IssueItem"
table="issueFeature">
<id name="id" column="id" type="int" unsaved-value="0">
<generator class="increment" />
</id>
<property name="description" />
<many-to-one name="issue"
column="issueId"
class="com.dfischer.primo.data.domain.Issue"
not-null="true"
cascade="save-update" />
</class>
</hibernate-mapping>
Full stack trace of any exception that occurs:Code:
Error creating bean with name 'sessionFactory' defined in class path resource [primo-data.xml]: Initialization of bean failed; nested exception is org.hibernate.MappingException: Foreign key (FK1596FE7E1598872B:everyIssue [elt])) must have same number of columns as the referenced primary key (everyIssue [id,elt])
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [primo-data.xml]: Initialization of bean failed; nested exception is org.hibernate.MappingException: Foreign key (FK1596FE7E1598872B:everyIssue [elt])) must have same number of columns as the referenced primary key (everyIssue [id,elt])
org.hibernate.MappingException: Foreign key (FK1596FE7E1598872B:everyIssue [elt])) must have same number of columns as the referenced primary key (everyIssue [id,elt])
at org.hibernate.mapping.ForeignKey.alignColumns(ForeignKey.java:86)
at org.hibernate.mapping.ForeignKey.setReferencedTable(ForeignKey.java:51)
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:940)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:888)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1036)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:678)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:613)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1114)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:349)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:257)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:147)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:271)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:319)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:80)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:65)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:56)
at com.dfischer.primo.data.dao.IssueDaoTest.setUp(IssueDaoTest.java:47)
at org.apache.commons.jelly.tags.ant.AntTag.doTag(AntTag.java:185)
at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:279)
at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:135)
at org.apache.commons.jelly.TagSupport.invokeBody(TagSupport.java:233)
at org.apache.commons.jelly.tags.core.IfTag.doTag(IfTag.java:88)
at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:279)
at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:135)
at org.apache.maven.jelly.tags.werkz.MavenGoalTag.runBodyTag(MavenGoalTag.java:79)
at org.apache.maven.jelly.tags.werkz.MavenGoalTag$MavenGoalAction.performAction(MavenGoalTag.java:110)
at com.werken.werkz.Goal.fire(Goal.java:639)
at com.werken.werkz.Goal.attain(Goal.java:575)
at com.werken.werkz.Goal.attainPrecursors(Goal.java:488)
at com.werken.werkz.Goal.attain(Goal.java:573)
at com.werken.werkz.Goal.attainPrecursors(Goal.java:488)
at com.werken.werkz.Goal.attain(Goal.java:573)
at com.werken.werkz.WerkzProject.attainGoal(WerkzProject.java:193)
at org.apache.maven.jelly.tags.werkz.MavenAttainGoalTag.doTag(MavenAttainGoalTag.java:127)
at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:279)
at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:135)
at org.apache.maven.jelly.tags.werkz.MavenGoalTag.runBodyTag(MavenGoalTag.java:79)
at org.apache.maven.jelly.tags.werkz.MavenGoalTag$MavenGoalAction.performAction(MavenGoalTag.java:110)
at com.werken.werkz.Goal.fire(Goal.java:639)
at com.werken.werkz.Goal.attain(Goal.java:575)
at org.apache.maven.plugin.PluginManager.attainGoals(PluginManager.java:671)
at org.apache.maven.MavenSession.attainGoals(MavenSession.java:263)
at org.apache.maven.cli.App.doMain(App.java:488)
at org.apache.maven.cli.App.main(App.java:1239)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at com.werken.forehead.Forehead.run(Forehead.java:551)
at com.werken.forehead.Forehead.main(Forehead.java:581)
Name and version of the database you are using:
MySQL v4.1.9