gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:31:55 25 4
gpt4 key购买 nike

我正在尝试使用 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)

RefundTransaction.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 或 @ManyToOne 注释中犯了一些错误。

更新:我的数据库中只有 2 个表,refund_transactionitem_refund_detail。而且我没有任何名为 refund_transaction_item_refund_detail 的表。规范化架构后,我在 item_refund_detail 表中添加了 refund_request_id 以维护关系。也许我需要维护单向映射才能实现这一点。请指教。

最佳答案

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

  1. 您正在使用字段访问权限和属性访问权限。您必须选择一种样式作为默认样式(然后您可以使用 AccessType 覆盖默认样式以获得所需的属性)。
  2. @ElementCollection 用于嵌入集合,因此会出现错误。为此,您应该使用 @OneToMany
  3. 不要忘记将双向关联的一侧声明为反向关联(在 @OneToMany 关系的情况下,通常是 One 上的实体侧)。

关于java - 使用 Hibernate、JPA 建立一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35040925/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com