public class LoginUser implements java.io.Serializable {
private String id;
private User user;
private String loginname;
private String passwd;
}
public class User implements java.io.Serializable {
private String userid;
private String name;
private int age;
}
为了省地,我没有写那些set,get方法
LoginUser.hbm.xml
<hibernate-mapping>
<class name="com.my.test.LoginUser" table="loginuser">
<id name="id" type="string" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="foreign">
<param name="property">user</param>
</generator>
</id>
<property name="loginname" />
<property name="passwd" />
<one-to-one name="user" class="com.my.test.User" constrained="true"/>
</class>
</hibernate-mapping>
User.hbm.xml
<class name="com.my.test.User" table="user2">
<id name="id" type="string" >
<column name="id" sql-type="char(32)" not-null="true"/>
<generator class="native"/>
</id>
<property name="name" />
<property name="age" />
</class>
当我SchemaExport LoginUser.hbm.xml时,总是异常
[java] net.sf.hibernate.MappingException: An association from the table loginuser refers to an unmapped class: com.my.test.User
[java] at net.sf.hibernate.cfg.Configuration.secondPassCompileForeignKeys(Configuration.java:682)
[java] at net.sf.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:666)
[java] at net.sf.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:459)
[java] at net.sf.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:58)
这是为什么? 什么情况总是出这种异常呢?
------解决方法--------------------------------------------------------
LoginUser.hbm.xml
User.hbm.xml
解析的时候,先解析LoginUser再解析User。而此时User尚未解析,SchemaExport找不到User类,必然报错。