Java 如何在hibernate中从一个表中获取实体列表?
考虑这个模式Java 如何在hibernate中从一个表中获取实体列表?,java,hibernate,one-to-many,Java,Hibernate,One To Many,考虑这个模式 Hospital ---------- id name email phone Department ---------- id name email phone Relation ---------- relationId Hospital_Id Department_id 这里每家医院都有多个科室。因此,医院与科室有OneToMany关系医院表包含医院列表部门表包含部门列表关系表连接它们。任何地方都没有使用外键 现在考虑这些PO
Hospital
----------
id
name
email
phone
Department
----------
id
name
email
phone
Relation
----------
relationId
Hospital_Id
Department_id
这里每家
医院都有多个
科室。因此,医院
与科室
有OneToMany
关系<代码>医院表包含医院列表<代码>部门表包含部门列表<代码>关系表连接它们。任何地方都没有使用外键
现在考虑这些POJOS(删除简短的不必要的东西)
还有,如何得到与此相反的结果,即如何在科室类中得到相应的医院对象?好了。希望能有帮助。干杯 编辑:Hibernate cfg文件位于: “rootDir\bin\hibernate.cfg.xml”
公共类实体管理器
{
//创建一个工厂,该工厂创建会话。
私营工厂;
/**
*构造函数。最初连接到数据库。
*/
公共实体管理器()
{
this.factory=new Configuration().configure(“hibernate.cfg.xml”)
.buildSessionFactory();
}
/**
*关闭会话工厂。
*/
公众假期结束()
{
this.factory.close();
}
/**
*从数据库加载实体的对象。
*/
公共列表加载实体()
{
Session Session=factory.getCurrentSession();
尝试
{
session.beginTransaction();
@抑制警告(“未选中”)
Query createQuery=会话
.createQuery(“来自您的实体”);
List result=createQuery.List();
session.getTransaction()
.commit();
返回结果;
}
最后
{
session.close();
}
}
/**
*按id加载实体。
*/
公共实体loadEntity(长实体ID)
{
Session Session=factory.getCurrentSession();
尝试
{
session.beginTransaction();
EntityA entity=session.get(EntityA.class,entityId);
返回实体;
}
最后
{
session.close();
}
}
/**
*将实体保存到数据库。
*/
公共作废保存实体(YourEntityA entityA)
{
Session Session=factory.getCurrentSession();
尝试
{
session.beginTransaction();
session.save(entityA);
session.getTransaction()
.commit();
}
最后
{
session.close();
}
}
}
我的eclipse中有可用的hibernate代码。不确定一对多请求如何影响它。如果您没有找到所需的答案,我可以向您展示我的实现。
public class Hospital {
...
@OneToMany
@JoinColumn(name="Hospital_ID")
private Set<Relation> relations;
...
}
public class Department{
...
@OneToMany
@JoinColumn(name="Department_ID")
private Set<Relation> relations;
...
}
public class Relation{
...
@Id
@Column(name = "relationId")
private int relationId;
@Column(name = "Hospital_ID")
private int hospitalId;
@Column(name = "Department_ID")
private int departmentId;
...
}
public Set<Departments> getDepartments() {
// Return set of departments corresponding to this(object) hospital
}
public class EntityManager
{
// Creates a factory, that creates sessions.
private SessionFactory factory;
/**
* Constructor. Initially connect to the database.
*/
public EntityManager()
{
this.factory = new Configuration().configure("hibernate.cfg.xml")
.buildSessionFactory();
}
/**
* Closes the session factory.
*/
public void close()
{
this.factory.close();
}
/**
* Loads the objects of your entity from the database.
*/
public List<YourEntity> loadEntities()
{
Session session = factory.getCurrentSession();
try
{
session.beginTransaction();
@SuppressWarnings("unchecked")
Query<YourEntity> createQuery = session
.createQuery("from YourEntity");
List<YourEntity> result = createQuery.list();
session.getTransaction()
.commit();
return result;
}
finally
{
session.close();
}
}
/**
* Loads entity by id.
*/
public EntityA loadEntity(long entityId)
{
Session session = factory.getCurrentSession();
try
{
session.beginTransaction();
EntityA entity = session.get(EntityA.class, entityId);
return entity;
}
finally
{
session.close();
}
}
/**
* Saves your entity to database.
*/
public void saveEntity(YourEntityA entityA)
{
Session session = factory.getCurrentSession();
try
{
session.beginTransaction();
session.save(entityA);
session.getTransaction()
.commit();
}
finally
{
session.close();
}
}
}