title: setting up one to many relationship using Hibernate, JPA
body: <p>I am trying to setup one to many relationship using Hibernate/Jpa.</p>
  
  <p>Currently I am getting this exception:</p>
  
  <pre><code>Invocation of init method failed; nested exception is org.hibernate.HibernateException: Missing table: refund_transaction_item_refund_detail
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1045)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:824)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
      at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:746)
      at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:238)
      at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1238)
      at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:689)
      at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:480)
      at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:256)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
      at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
      at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:172)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
      at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
      at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
      at org.eclipse.jetty.server.Server.doStart(Server.java:279)
      at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:65)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
      at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:520)
      at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:365)
      at org.mortbay.jetty.plugin.JettyRunWarMojo.execute(JettyRunWarMojo.java:71)
      at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
      at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
      at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
      at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
      at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
      at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
      at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
      at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
      at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:347)
      at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:154)
      at org.apache.maven.cli.MavenCli.execute(MavenCli.java:582)
      at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
      at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
      at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
      at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
      at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
  Caused by:
  org.hibernate.HibernateException: Missing table: refund_transaction_item_refund_detail
      at org.hibernate.cfg.Configuration.validateSchema(Configuration.java:1333)
      at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:155)
      at org.hibernate.internal.SessionFactoryImpl.&lt;init&gt;(SessionFactoryImpl.java:525)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
      at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:397)
      at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
      at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
      at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
      at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
      at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1045)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:824)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
      at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
      at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
      at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
      at org.eclipse.jetty.server.handler.ContextHandler.startContext(ContextHandler.java:746)
      at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:238)
      at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1238)
      at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:689)
      at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:480)
      at org.mortbay.jetty.plugin.JettyWebAppContext.doStart(JettyWebAppContext.java:256)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
      at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
      at org.eclipse.jetty.server.handler.ContextHandlerCollection.doStart(ContextHandlerCollection.java:172)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
      at org.eclipse.jetty.server.handler.HandlerCollection.doStart(HandlerCollection.java:229)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
      at org.eclipse.jetty.server.handler.HandlerWrapper.doStart(HandlerWrapper.java:95)
      at org.eclipse.jetty.server.Server.doStart(Server.java:279)
      at org.mortbay.jetty.plugin.JettyServer.doStart(JettyServer.java:65)
      at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:64)
      at org.mortbay.jetty.plugin.AbstractJettyMojo.startJetty(AbstractJettyMojo.java:520)
      at org.mortbay.jetty.plugin.AbstractJettyMojo.execute(AbstractJettyMojo.java:365)
      at org.mortbay.jetty.plugin.JettyRunWarMojo.execute(JettyRunWarMojo.java:71)
      at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
      at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
      at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
      at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
      at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
      at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
      at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
      at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
      at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:347)
      at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:154)
      at org.apache.maven.cli.MavenCli.execute(MavenCli.java:582)
      at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:214)
      at org.apache.maven.cli.MavenCli.main(MavenCli.java:158)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:606)
      at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
      at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
      at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
      at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
  </code></pre>
  
  <p><strong>RefundTransaction.java</strong> This is the parent class</p>
  
  <pre><code>import java.util.Date;
  import java.util.Set;
  
  import javax.persistence.CascadeType;
  import javax.persistence.Column;
  import javax.persistence.ElementCollection;
  import javax.persistence.Entity;
  import javax.persistence.FetchType;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  import javax.persistence.Temporal;
  import javax.persistence.TemporalType;
  
  import org.hibernate.annotations.GenericGenerator;
  
  import com.limeroad.commons.RefundRequest;
  
  
  @Entity
  @Table(name = "refund_transaction")
  public class RefundTransaction {
  
    public RefundTransaction() {}
  
    public RefundTransaction(RefundRequest refundRequest) {
      setOrderId(refundRequest.getOrderId());
      setRefundReason(refundRequest.getRefundReason());
      setCallbackUrl(refundRequest.getCallBackUrl());
      setCreatedBy(refundRequest.getCreatedBy());
      setIdempotenceKey(refundRequest.getClientName() + ":" + refundRequest.getClientKey());
    }
  
    @ElementCollection(targetClass = ItemRefundDetail.class)
    Set&lt;ItemRefundDetail&gt; itemRefundDetails;
  
    @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
    @JoinColumn(name = "refund_request_id", referencedColumnName = "refund_request_id" )
    public Set&lt;ItemRefundDetail&gt; getItemRefundDetails() {
      return itemRefundDetails;
    }
  
    public void setItemRefundDetails(Set&lt;ItemRefundDetail&gt; itemRefundDetails) {
      this.itemRefundDetails = itemRefundDetails;
    }
  
    @Column(name = "refund_reason")
    String refundReason;
  
    @Column(name = "callback_url")
    String callbackUrl;
  
    @Column(name = "order_id")
    String orderId;
  
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "com.limeroad.services.payments.factory.LimeroadIdGenerator")
    @Column(name = "refund_request_id")
    String refundRequestId;
  
    @Column(name = "idempotence_key")
    String idempotenceKey;
  
    @Column(name = "created_by")
    String createdBy;
  
    @Column(name = "modified_by")
    String modifiedBy;
  
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "create_date")
    Date createDate;
  
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_modified")
    Date lastModified;
  
    @Column(name = "refund_reason")
    public String getRefundReason() {
      return refundReason;
    }
  
    @Column(name = "callback_url")
    public String getCallbackUrl() {
      return callbackUrl;
    }
  
    @Column(name = "order_id")
    public String getOrderId() {
      return orderId;
    }
  
    @Column(name = "refund_request_id")
    public String getRefundRequestId() {
      return refundRequestId;
    }
  
    @Column(name = "idempotence_key")
    public String getIdempotenceKey() {
      return idempotenceKey;
    }
  
    @Column(name = "created_by")
    public String getCreatedBy() {
      return createdBy;
    }
  
    @Column(name = "modified_by")
    public String getModifiedBy() {
      return modifiedBy;
    }
  
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_modified")
    public Date getCreateDate() {
      return createDate;
    }
  
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_modified")
    public Date getLastModified() {
      return lastModified;
    }
  
    public void setRefundReason(String refundReason) {
      this.refundReason = refundReason;
    }
  
    public void setCallbackUrl(String callbackUrl) {
      this.callbackUrl = callbackUrl;
    }
  
    public void setOrderId(String orderId) {
      this.orderId = orderId;
    }
  
    public void setRefundRequestId(String refundRequestId) {
      this.refundRequestId = refundRequestId;
    }
  
    public void setIdempotenceKey(String idempotenceKey) {
      this.idempotenceKey = idempotenceKey;
    }
  
    public void setCreatedBy(String createdBy) {
      this.createdBy = createdBy;
    }
  
    public void setModifiedBy(String modifiedBy) {
      this.modifiedBy = modifiedBy;
    }
  
    public void setCreateDate(Date createDate) {
      this.createDate = createDate;
    }
  
    public void setLastModified(Date lastModified) {
      this.lastModified = lastModified;
    }
  
  
  
  }
  </code></pre>
  
  <p><strong>ItemRefundDetail.java</strong> This is the child class.</p>
  
  <pre><code>import java.io.Serializable;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.ManyToOne;
  import javax.persistence.Table;
  import javax.persistence.Transient;
  
  @Table(name = "item_refund_detail")
  @Entity
  public class ItemRefundDetail implements Serializable {
    private static final long serialVersionUID = 4604092182622619714L;
  
    RefundTransaction refundTransaction;
  
    @ManyToOne
    @JoinColumn(name="refund_request_id")
    public RefundTransaction getRefundTransaction() {
      return refundTransaction;
    }
  
    public void setRefundTransaction(RefundTransaction refundTransaction) {
      this.refundTransaction = refundTransaction;
    }
  
    @Id
    @Column(name = "order_id")
    String orderId;
  
    @Id
    @Column(name = "unique_item_id")
    String uniqueItemId;
  
    Double amount;
  
  //  @Column(name = "refund_request_id")
  //  String refundRequestId;
  
    @Id
    @Column(name = "refund_transaction_id")
    String refundTransactionId;
  
    @Column(name = "transaction_ref")
    String transactionRef;
  
    @Transient
    InstrumentType instrumentType;
  
    @Id
    @Column(name = "order_id")
    public String getOrderId() {
      return orderId;
    }
  
    @Id
    @Column(name = "unique_item_id")
    public String getUniqueItemId() {
      return uniqueItemId;
    }
  
  
    public Double getAmount() {
      return amount;
    }
  
  //  @Column(name = "refund_request_id")
  //  public String getRefundRequestId() {
  //    return refundRequestId;
  //  }
  
    @Id
    @Column(name = "refund_transaction_id")
    public String getRefundTransactionId() {
      return refundTransactionId;
    }
  
    @Column(name = "transaction_ref")
    public String getTransactionRef() {
      return transactionRef;
    }
  
    @Transient
    public InstrumentType getInstrumentType() {
      return instrumentType;
    }
  
    public void setOrderId(String orderId) {
      this.orderId = orderId;
    }
  
    public void setUniqueItemId(String uniqueItemId) {
      this.uniqueItemId = uniqueItemId;
    }
  
    public void setAmount(Double amount) {
      this.amount = amount;
    }
  
  //  public void setRefundRequestId(String refundRequestId) {
  //    this.refundRequestId = refundRequestId;
  //  }
  
    public void setRefundTransactionId(String refundTransactionId) {
      this.refundTransactionId = refundTransactionId;
    }
  
    public void setTransactionRef(String transactionRef) {
      this.transactionRef = transactionRef;
    }
  
    public void setInstrumentType(InstrumentType instrumentType) {
      this.instrumentType = instrumentType;
    }
  
  
  
  }
  </code></pre>
  
  <p>I know I am making some mistake in @OneToMany or @ManyToOne annotation. </p>
  
  <p>UPDATE:
  I only have 2 tables in the db, <strong>refund_transaction</strong> and <strong>item_refund_detail</strong>. And I dont't have any table named <strong>refund_transaction_item_refund_detail</strong>. After normalizing the schema, I have added <strong>refund_request_id</strong> in the <strong>item_refund_detail</strong> table for maintaining the relationship.
  Probably I need to maintain unidirectional mapping, to achieve this. Please advice.</p>