hibernate.cf.xml文件
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">create</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="connection.username">system</property>
<property name="connection.password">Password</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<mapping class="com.mypackage.UserDetails"/>
<!-- <mapping class="com.mypackage.Address1"/> -->
</session-factory>
</hibernate-configuration>
UserDetails实体
package com.mypackage;
@Entity
@Table(name="UserDetailsTable")
public class UserDetails {
@Id
//@GeneratedValue//(strategy = GenerationType.AUTO)
private int userId;
private String userName;
@Embedded
private Address1 address;
private String phoneNo;
private Date date;
}
可嵌入实体
package com.mypackage;
@Embeddable
public class Address1 {
private String street;
private String city;
private String state;
private String pincode;
}
主要类
public class Test {
public static void main(String[] args) {
Session session=new AnnotationConfiguration().
configure().buildSessionFactory().openSession();
Transaction t=session.beginTransaction();
Address1 addrobj1=new Address1();
addrobj1.setStreet("karunechi");
addrobj1.setCity("Edakkara");
addrobj1.setState("Malappuram");
addrobj1.setPincode("679331");
UserDetails obj1=new UserDetails();
obj1.setUserId(1);
obj1.setUserName("kannan s nair");
obj1.setAddress(addrobj1);
obj1.setPhoneNo("9633835509");
session.saveOrUpdate(addrobj1);
t.commit();
session.close();
System.out.println("saved successfully");
}
}
错误上eclipse juno是:
log4j:WARN找不到记录器的appender (org.hibernate.cfg.annotations.Version)。 log4j:WARN请初始化 log4j系统正常。线程“main”中的异常 org.hibernate.MappingException:未知实体:com.mypackage.Address1 在 org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:548) 在 org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1338) 在org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:180) 在 org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:487) 在 org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:84) 在 org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70) 在 org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507) 在org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499) 在org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495) 在com.mypackage.Test.main(Test.java:33)
无法解决错误。
有人请帮帮忙吗?
答案 0 :(得分:1)
您已对地图类别<!-- <mapping class="com.mypackage.Address1"/> -->
进行了评论,因此无法找到它
答案 1 :(得分:0)
请将Address1 bean的映射标记添加到您的hibernate配置文件中,如果您创建了Address1 bean的映射文件,则创建它。
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Address1" table="ADDRESS1">
<meta attribute="class-description">
This class contains the address detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="street" column="street_name" type="string"/>
<property name="city" column="city_name" type="string"/>
<property name="state" column="state_name" type="string"/>
<property name="zipcode" column="zipcode" type="string"/>
</class>
</hibernate-mapping>
您还可以使用以下标记在配置文件中添加实体映射
<mapping resource>com/mypackage/Address1.hbm.xml</mapping resource>
或者您必须使用
将类显式添加到配置中configuration.addClass(com.mypackage.Address1.class)
// Read mappings as a application resourceName
// addResource is for add hbml.xml files in case of declarative approach
configuration.addResource("com.mypackage.Address1.xml")