【发布时间】:2013-12-05 10:37:51
【问题描述】:
我有一个城市和人之间的一对多映射。一个城市可以有很多人。 在持久化数据之后,在从休眠中获取 People 对象时,我也得到了城市对象。城市是父表/实体。 但是在使用 hibernate 获取城市对象时,人员对象(集合或列表)是空的。对 MYSQL DB 尝试相同的代码时可以正常工作。但是在内存数据库中使用 HSQL 时,我遇到了这个问题。 PFB详情:-
城市实体:-
@Entity
@Table(name="CITY")
public class City {
@Id
@GeneratedValue
@Column(name="CITY_ID")
private long id;
@Column(name="CITY_NAME", nullable=false, unique= true)
private String name;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "city" )
private Set<People> people = new HashSet<People>(0);
人民实体:-
@Entity
@Table(name="People")
public class People {
@Id
@GeneratedValue
@Column(name="People_ID")
private long id;
@Column(name="People_NAME", nullable=false, unique= true)
private String name;
@ManyToOne
@JoinColumn(name = "CITY_ID")
City city;
我将首先将值插入到 City 表中。示例值:-(“London”)..如果生成的 cityid 为“1001”,则在持久化之后 然后将值插入到人员表中。示例值:-("John",1001) 然后在使用 hibernate 获取 City 对象的值时, people(set) 为空。 但是,如果我获取 People 对象的值,则 city 对象具有值。 PFb 我的 xml 文件:-
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:TEST" />
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="entity" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
这是内存数据库的限制,还是我遗漏了什么。 任何建议或链接都会有所帮助
【问题讨论】:
-
我猜这会导致在数据库中只创建两个表。正确的??您在数据库中没有任何关系表。
标签: java mysql hibernate hibernate-mapping hsqldb