我正在尝试使用JPA& amp;来构建一个小项目。 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);
}
只有当我摆脱Customer和Rent实体之间的关系时,代码才能正常工作,这实际上是通过阻止引用Customer实体中的私有List租赁的代码来实现一对多。 如果我试试这个就会给我这个错误
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会话练习过这个,但我需要使用JPA Annotation和JPA EntityManager来实现这一点。
我在项目中包含了hibernate库,但没有区别。
这是我对Enterprise Java的第一次尝试,我对自己的学习能力感到沮丧。 希望你们能帮助我。
如果有人能给我一个实现DVD租赁商店数据库的EJB + JPA项目的示例代码,我将非常感激。我这样做是为了得到一份工作(我已经失去了机会......但我真的想完成这个以备将来的机会)。
帖子太长了。感谢您阅读本文。 :)
**这是我的jar文件列表
答案 0 :(得分:1)
你的类路径中至少缺少一个Hibernate jar:hibernate-core。至少你需要hibernate-entitymanager + hibernate-core +。
TBH这就是为什么项目通常采用某种形式的依赖管理,如Maven或Gradle或Ivy或......