我遇到了hibernate的问题。我正在更新在Oracle SQL中创建的以前创建的数据库,以便在hibernate中进行一些练习。问题是我得到一个AnnotationException,一个类对象试图引用另一个类的东西。这是错误:
org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.revature.bank.POJO.BankAccount.customer references an unknown entity: com.revature.bank.POJO.Customer
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:107)
at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1580)
at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1503)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1419)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1856)
at com.revature.bank.POJO.DataFuncImp.createCustomer(DataFuncImp.java:16)
at com.revature.bank.POJO.Main.main(Main.java:9)
所以,这指向了一行:
SessionFactory sf = new Configuration().configure().buildSessionFactory();
我已经读到它可能正在发生,因为注释但这里没有任何先前提出的问题似乎有效。 这是我的两个类:(我只会将变量作为错误指向的变量): Customer.java
@Entity
@Table(name="USER_TABLE")
public class Customer {
@Id
@GeneratedValue
private int user_id;
@Column
private String user_fname;
@Column
private String user_lname;
@Column
private String user_email;
@Column
private String user_address;
@Column
private String user_city;
@Column
private String user_state;
@Column
private long cell_num;
@OneToMany(cascade= {CascadeType.ALL}, mappedBy="customer")
@JoinColumn(name="user_id")
private List<BankAccount> bacct;
BankAccount.java
@Entity
@Table(name="USER_ACCOUNT")
public class BankAccount {
@Id
@GeneratedValue
private long acct_id;
@Column
private double balance;
@ManyToOne
@JoinColumn(name="user_id")
private Customer customer;
这里也是我的hibernate配置文件(编辑出数据库连接信息):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">adminone</property>
<property name="hibernate.connection.password">adminpass</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle11gDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<!-- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> -->
<!-- <mapping resource="students.hbm.xml"></mapping> -->
<mapping class="com.revature.bank.POJO.Customer"></mapping>
<mapping class="com.revature.bank.POJO.BankAccount"></mapping>
</session-factory>
</hibernate-configuration>
我正在使用hibernate 3.0。就像我之前提到的那样,我一直试图弄清楚错误,但是没有任何在线帮助似乎能解决它。正如你所看到的,我甚至都没有尝试对数据库做任何事情而且它正在抛出异常。知道为什么会这样吗?提前谢谢!!
答案 0 :(得分:0)
你不应该双方都有JoinColumn。只是在所有者方面,所以将其从Customer类中删除。
Customer.java:
@OneToMany(cascade= {CascadeType.ALL}, mappedBy="customer")
private List<BankAccount> bacct;
BankAccount.java:
@ManyToOne
@JoinColumn(name="user_id")
private Customer customer;