gpt4 book ai didi

mysql - (JPA) 无法使用 hibernate.hbm2ddl.auto 正确实现多对一关系

转载 作者:行者123 更新时间:2023-11-29 13:49:19 26 4
gpt4 key购买 nike

我试图使用 JPA 和 EJB 构建一个小项目,但我已经陷入了非常早期的阶段......

问题是这样的。

我有三个实体- 顾客- 元素- 出租

正如您可以从名称中得出的那样,客户租赁(多对一)和元素租赁(多对一)。因此,Rental 实体将 Customer 和 Item 实体的 ID 存储为外键。

我通过在 persistence.xml 中使用 hibernate.hbm2ddl.auto 属性实现了这一点

    <persistence-unit name="JPADB">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/MySQLDS</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.format_sql" value="true" />
<property name="showSql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>

客户

@Entity
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
public Customer() {super();}

@Id
@Column(name = "customer_id")
@GeneratedValue
private int customerId;
@Column(name = "customer_name")
private String customerName;
@OneToMany(targetEntity=Rental.class, mappedBy="customer", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private List<Rental> rentals;

项目

@Entity
public class Item implements Serializable{
private static final long serialVersionUID = 1L;
public Item() {super();}

@Id
@Column(name = "item_id")
@GeneratedValue
private int itemId;
@Column(name = "item_name")
private String itemName;
@OneToMany(targetEntity=Rental.class, mappedBy="item", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private List<Rental> rentals;

出租

@Entity
public class Rental implements Serializable{
private static final long serialVersionUID = 1L;
public Rental(){super();}

@Id
@Column(name = "rental_id")
@GeneratedValue
private int rentalId;
private String rentalName;
@ManyToOne(targetEntity = Customer.class)
@JoinColumn(name="customer_id", nullable = false)
private Customer customer;
@ManyToOne(targetEntity = Item.class)
@JoinColumn(name="item_id", nullable = false)
private Item item;

我用下面的代码测试它

@Override
public void saveCustomer(Customer customer) {
if(entityManager.contains(customer))
entityManager.merge(customer);
else
entityManager.persist(customer);
}

只有当我通过阻止在客户实体中引用私有(private)列表租赁的代码来摆脱客户和租赁实体之间的关系(这是一对多)时,代码才能正常工作。如果我尝试这样做,它会给我这个错误

log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy0.retrieveAllItems(Unknown Source)
at com.hidvd.client.HiDVDApplicationClient.main(HiDVDApplicationClient.java:28)
Caused by: java.lang.ClassNotFoundException: org.hibernate.collection.internal.PersistentBag
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.jboss.marshalling.AbstractClassResolver.loadClass(AbstractClassResolver.java:135)
at org.jboss.marshalling.AbstractClassResolver.resolveClass(AbstractClassResolver.java:116)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadClassDescriptor(RiverUnmarshaller.java:892)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadNewObject(RiverUnmarshaller.java:1204)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:272)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:209)
at org.jboss.marshalling.river.RiverUnmarshaller.readFields(RiverUnmarshaller.java:1677)
at org.jboss.marshalling.river.RiverUnmarshaller.doInitSerializable(RiverUnmarshaller.java:1593)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadNewObject(RiverUnmarshaller.java:1235)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:272)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:209)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadCollectionObject(RiverUnmarshaller.java:180)
at org.jboss.marshalling.river.RiverUnmarshaller.readCollectionData(RiverUnmarshaller.java:771)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:649)
at org.jboss.marshalling.river.RiverUnmarshaller.doReadObject(RiverUnmarshaller.java:209)
at org.jboss.marshalling.AbstractObjectInput.readObject(AbstractObjectInput.java:37)
at org.jboss.ejb.client.remoting.MethodInvocationResponseHandler$MethodInvocationResultProducer.getResult(MethodInvocationResponseHandler.java:107)
at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:270)
at org.jboss.ejb.client.TransactionInterceptor.handleInvocationResult(TransactionInterceptor.java:47)
at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:272)
at org.jboss.ejb.client.ReceiverInterceptor.handleInvocationResult(ReceiverInterceptor.java:132)
at org.jboss.ejb.client.EJBClientInvocationContext.getResult(EJBClientInvocationContext.java:260)
at org.jboss.ejb.client.EJBClientInvocationContext.awaitResponse(EJBClientInvocationContext.java:399)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:140)
at org.jboss.ejb.client.EJBInvocationHandler.doInvoke(EJBInvocationHandler.java:121)
at org.jboss.ejb.client.EJBInvocationHandler.invoke(EJBInvocationHandler.java:104)
... 2 more

我之前已经使用 hibernate session 练习过此操作,但我需要使用 JPA Annotation 和 JPA EntityManager 来完成此操作。

我在项目中包含了 hibernate 库,但没有区别。

这是我第一次尝试 Enterprise Java,我对自己的学习能力感到沮丧。希望你们能帮助我。

如果有人能给我一个实现 DVD 租赁商店数据库的 EJB+JPA 项目的示例代码,我将非常感激。我这样做是为了找到一份工作(我已经失去了这个机会......但我真的很想完成这个工作以获得 future 的机会)。

帖子太长了。感谢您阅读本文。 :)

**这是我的 jar 文件列表 enter image description here

最佳答案

您的类路径中至少缺少一个 Hibernate jar:hibernate-core。至少你需要 hibernate-entitymanager + hibernate-core + 。

说实话,这就是为什么项目通常采用某种形式的依赖管理,无论是 Maven、Gradle、Ivy 还是......

关于mysql - (JPA) 无法使用 hibernate.hbm2ddl.auto 正确实现多对一关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16906768/

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