首页 > 解决方案 > 如何在休眠中从一张表中获取实体列表?

问题描述

考虑这个模式

Hospital
----------
  id
  name
  email
  phone

Department
----------
  id
  name
  email
  phone

Relation
----------
  relationId
  Hospital_Id
  Department_id

这里each医院有many科室。所以HospitalOneToMany关系DepartmentsHospital表有医院列表。Department表有部门列表。Relation表加入他们。任何地方都没有使用外键。

现在考虑这些 pojos(为简洁起见,删除了不必要的东西)

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;    
    ...
}

所以我的问题是我如何获得任何给定医院的部门列表(或集合)?那就是我想在Hospitalpojo中实现这个方法

public Set<Departments> getDepartments() {
    // Return set of departments corresponding to this(object) hospital
}

还有我如何得到相反的结果,即如何在部门类中获得相应的医院对象?

标签: javahibernateone-to-many

解决方案


你去吧。希望能帮助到你。干杯!

编辑:休眠 cfg 文件位于:

“rootDir\bin\hibernate.cfg.xml”

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();
        }
    }
}

推荐阅读