请教一个关于hibername的many-to-one的问题。
我定义了两各类:event ,location类。event包含了一个location类的对象Event.hbm.xml的配置文件如下:
<class name="Event" table="events">
<id name="id" type="java.lang.Integer">
<generator class="increment"></generator>
</id>
<property name="name" type="java.lang.String"></property>
<!--<property name="startDate" column="start_date" type="date"></property> -->
<property name="duration" column="duratiion" type="java.lang.Integer"></property>
<property name="startDate" column="startDate" type="java.lang.String"></property>
<many-to-one name="location" column="location_id" class="Location"></many-to-one>
</class>
Location.hbm.xml的配置文件如下
<class name="Location" table="locations">
<id name="id" type="java.lang.Integer">
<generator class="increment"></generator>
</id>
<property name="name" type="java.lang.String"></property>
<property name="address" type="java.lang.String"></property>
</class>
问题是:当我持久化这两个类后,"location_id"字段为空,其他字段都很正常。请问这是怎么回事?
我感觉好像是配置文件好像少其他的配置语句。
我的测试类的内容如下:
Configuration configuraton;
SessionFactory sessionFactory;
Session session;
// 实例化Configuration
configuraton=new Configuration().configure();
// 实例化SessionFactory
sessionFactory =configuraton.buildSessionFactory();
// 实例化Session
session = sessionFactory.openSession();
Event event=new Event();
Location location=new Location();
event.setName("first");
event.setStartDate("21-12月-83");
event.setDuration(10);
location.setName("china");
location.setAddress("china");
Transaction tx = session.beginTransaction();
session.save(event);
session.save(location);
();
session.flush();
session.clear();