An association from the table PersonAddress refers to an unmapped class
作者:互联网
cjq\hibernate\tutorial\domain01\Person.hbm.xml文件内容为:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cjq.hibernate.tutorial.domain01">
<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<join table="PersonAddress" optional="true">
<key column="personId" />
<many-to-one name="address" column="addressId" not-null="true"/>
</join>
</class>
<class name="Address">
<id name="id" column="addressId">
<generator class="native" />
</id>
</class>
</hibernate-mapping>
Person.java
package cjq.hibernate.tutorial.domain01;
import java.util.HashSet;
import java.util.Set;
public class Person {
int id;
int address ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAddress() {
return address;
}
public void setAddress(int address) {
this.address = address;
}
}
Address.java
package cjq.hibernate.tutorial.domain01;
public class Address {
int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
MyTest.java
package cjq.hibernate.tutorial.domain01;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import cjq.hibernate.tutorial.util.HibernateUtil;
public class MyTest {
public static void main(String [] args){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Address a = new Address();
session.save(a);
Address b = new Address();
session.save(b);
Person p1 = new Person();
p1.setAddress(a.getId());
session.save(p1);
Person p2 = new Person();
p2.setAddress(a.getId());
session.save(p2);
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
}
}
运行MyTest.java
此时报错:
Initial SessionFactory creation failed.org.hibernate.MappingException: An association from the table PersonAddress refers to an unmapped class: int
Exception in thread "main" java.lang.ExceptionInInitializerError
at cjq.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:27)
at cjq.hibernate.tutorial.util.HibernateUtil.<clinit>(HibernateUtil.java:15)
at cjq.hibernate.tutorial.domain01.MyTest.main(MyTest.java:12)
Caused by: org.hibernate.MappingException: An association from the table PersonAddress refers to an unmapped class: int
at org.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:1697)
at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1631)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1366)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1731)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1782)
at cjq.hibernate.tutorial.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:23)
... 2 more
分析后,Person.java中,address的类型不应该为int,而应该为Address类型
修改后的Person.java为:
package cjq.hibernate.tutorial.domain01;
import java.util.HashSet;
import java.util.Set;
public class Person {
int id;
Address address ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
同时,MyTest.java的内容做相应修改:
package cjq.hibernate.tutorial.domain01;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import cjq.hibernate.tutorial.util.HibernateUtil;
public class MyTest {
public static void main(String [] args){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Address a = new Address();
session.save(a);
Address b = new Address();
session.save(b);
Person p1 = new Person();
p1.setAddress(a);
session.save(p1);
Person p2 = new Person();
p2.setAddress(a);
session.save(p2);
session.getTransaction().commit();
HibernateUtil.getSessionFactory().close();
}
}
此时,再运行MyTest.java
输出的错误信息为:
ERROR: HHH000388: Unsuccessful: create table PersonAddress (personId number(10,0) not null unique, addressId number(10,0) not null, primary key (personId))
八月 25, 2012 11:10:25 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: ORA-02261: 表中已存在这样的唯一关键字或主键
八月 25, 2012 11:10:25 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000388: Unsuccessful: alter table PersonAddress add constraint FKA81B4EBF9A4AE9FE foreign key (personId) references Person
八月 25, 2012 11:10:25 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: ORA-00942: 表或视图不存在
八月 25, 2012 11:10:25 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: HHH000388: Unsuccessful: alter table PersonAddress add constraint FKA81B4EBF945FDFAA foreign key (addressId) references Address
八月 25, 2012 11:10:25 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
ERROR: ORA-00942: 表或视图不存在
八月 25, 2012 11:10:25 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into Address (addressId) values (?)
Hibernate: insert into Address (addressId) values (?)
Hibernate: insert into Person (personId) values (?)
Hibernate: insert into PersonAddress (addressId, personId) values (?, ?)
八月 25, 2012 11:10:26 下午 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 942, SQLState: 42000
八月 25, 2012 11:10:26 下午 org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-00942: 表或视图不存在
Exception in thread "main" org.hibernate.exception.SQLGrammarException: ORA-00942: 表或视图不存在
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:122)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)
at $Proxy6.executeUpdate(Unknown Source)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2862)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3300)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:88)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:362)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:354)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:275)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:326)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:52)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1214)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:403)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at cjq.hibernate.tutorial.domain01.MyTest.main(MyTest.java:36)
Caused by: java.sql.SQLException: ORA-00942: 表或视图不存在
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:745)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216)
at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:966)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1170)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3339)
at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3423)
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:601)
at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
... 15 more
注意到:create table PersonAddress (personId number(10,0) not null unique, addressId number(10,0) not null, primary key (personId))这个建表语句,primary key 与 not null unique 是重复的。
查看Person.hbm.xml,发现:
<join table="PersonAddress" optional="true">
<key column="personId" unique="true"/>
<many-to-one name="address" column="addressId" not-null="true"/>
</join>
此处的unique="true"应该去掉:
<join table="PersonAddress" optional="true">
<key column="personId" />
<many-to-one name="address" column="addressId" not-null="true"/>
</join>
这样再次运行MyTest.java
就正确了。
转载于:https://my.oschina.net/u/2552902/blog/543860
标签:hibernate,java,Person,jdbc,org,unmapped,table,refers,public 来源: https://blog.csdn.net/weixin_33882452/article/details/92327067