0

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

这就是问题所在。

我有三个实体 - 客户 - 项目 - 租赁

正如您可以从名称中得出的那样,Customer-Rental(多对一)和 Item-Rental(多对一)。因此,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);
    }

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

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

我以前使用休眠会话练习过这个,但我需要使用 JPA Annotation 和 JPA EntityManager 来做到这一点。

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

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

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

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

**这是我的 jar 文件列表 在此处输入图像描述

4

1 回答 1

1

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

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

于 2013-06-03T22:53:29.060 回答