当前位置: 首页 > 知识库问答 >
问题:

使用Hibernate、JPA建立一对多的关系

司徒翼
2023-03-14

我正在尝试使用Hibernate/Jpa建立一对多关系。

当前我遇到以下异常:

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.<init>(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)

退款交易。java这是父类

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<ItemRefundDetail> itemRefundDetails;

  @OneToMany(fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
  @JoinColumn(name = "refund_request_id", referencedColumnName = "refund_request_id" )
  public Set<ItemRefundDetail> getItemRefundDetails() {
    return itemRefundDetails;
  }

  public void setItemRefundDetails(Set<ItemRefundDetail> 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;
  }



}

ItemRefundDetail.java这是子类。

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;
  }



}

我知道我在@OneToMany或@manytone注释中犯了一些错误。

更新:我在db、refund_transaction和item_refund_detail中只有2个表。我没有任何名为refund_transaction_item_refund_detail的表。规范化模式后,我在item_refund_detail表中添加了refund_request_id来维护关系。可能我需要维护单向映射,才能实现这一点。请建议。

共有1个答案

郜俊健
2023-03-14

你做了很多错事。其中一些要点:

  1. 您同时使用字段和属性访问。您必须选择一种样式作为默认样式(然后可以使用AccessType覆盖所需属性的默认样式)
  2. ElementCollection用于嵌入式集合,因此会出现错误。为此,您应该使用OneToMany
  3. 不要忘记将双向关联的一侧声明为反向关联(如果是一个一域关系,通常是一方的实体)
 类似资料:
  • 问题内容: 我有以下实体关系问题。一个“游戏”必须有两个(只有两个)“团队”对象。一个“团队”可以有很多“游戏” 据我所知,这是一对多关系。但是…我不知道如何在JPA中对此建模。例如,我打算做这样的事情… 但是,正如您所看到的,我不确定如何从注释侧将表链接在一起。有人曾经做过这样的事情吗?有什么想法吗? 非常感谢! 问题答案: 我希望有人能提出一个很棒的解决方案,但是这是一个棘手的情况,我从未能找

  • 问题内容: 我有2张桌子: 电影:movieID 用户:userID 这些表通过Queue表具有多对多关系,并带有一个附加属性listOrder: 队列:movieID,userID,listOrder 我正在尝试使用EclipseLink对此模型建模,但是却收到“不兼容映射”错误。这是我的代码的示例: QueueItemPK的目的是使我可以拥有movieID和userID的复合主键。我不确定这是

  • 如果在中只有一组,这将非常好地工作。ASSESSMENT_COMMENT只有两列: 它将完美地代表一对多的关系。 现在问题来了: 那么,1)这是Hibernate中已知的bug吗?2)有办法解决这个问题吗?我可以强制Hibernate创建两个映射表,每个映射表一个吗?请记住,我不能更改类来引用(业务逻辑需求)

  • 问题内容: 我有一对一的关系,但是hibernatetool在生成模式时抱怨。这是显示问题的示例: 人与OtherInfo具有一对一关系: 人是OtherInfo的拥有方。OtherInfo是拥有方,因此person用于在Person中指定属性名称“ otherInfo”。 使用hibernatetool生成数据库架构时出现以下错误: 知道为什么吗?我是在做错什么还是这是Hibernate错误?

  • 我在hibernate中搜索建立关系的各种可能性,遇到了下面的代码片段

  • 用户表结构:用户 id、名称、用户名、密码、创建时间、更新时间 文章表结构:文章 id、标题、内容、创建时间、更新时间 关系表:文章\用户 id、文章id、用户id处于活动状态、创建时间、更新时间 标签 id、名称、用户id、创建时间、更新时间 透视表项目用户与标记的关系。表:文章\用户\标签 标签号,物品号,用户号 我想连接这些表,以便可以像这样或类似的格式访问 并且应该能够创建/更新smth,