gpt4 book ai didi

java - 一对多双向关系在内存 HSQL DB 中不起作用

转载 作者:行者123 更新时间:2023-11-29 03:03:07 25 4
gpt4 key购买 nike

我有一个城市和人之间的一对多映射。一个城市可以有很多人。持久化数据后,从 hibernate 中获取 People 对象时,我也获取了城市对象。城市是父表/实体。但是在使用 hibernate 获取 city 对象时,people 对象(集合或列表)是空的。对 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”,则在坚持之后然后将值插入 people 表。示例值:-("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>

这是内存数据库的限制,还是我遗漏了什么。任何建议或链接都​​会有帮助

最佳答案

我最近创建了一个示例应用程序来演示这种使用 hibernate 的关系,下面是它的代码。

@Entity
@Table(name = "COMPANY")
public class Company {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "COMP_NAME", length = 1000)
private String companyName;
@Column(name = "COMP_ADDRESS", length = 1000)
private String companyAddress;
@OneToOne(mappedBy = "company")
private Employee emp;

@OneToMany
private Set<Department> departments = new HashSet<Department>();

public Set<Department> getDepartments() {
return departments;
}

public void setDepartments(Set<Department> departments) {
this.departments = departments;
}

下面是我的Department.java

@Entity
@Table(name = "DEPARTMENT")
public class Department {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String deptName;
@ManyToOne
private Company company;

public Company getCompany() {
return company;
}

public void setCompany(Company company) {
this.company = company;
}

请注意,我没有使用 @JoinColumn,这导致了关系表的创建,只需从您的 People 类中删除 @JoinColumn,您就会从 City 类中获取人员集合。

关于java - 一对多双向关系在内存 HSQL DB 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20112836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com