休眠关系OneToMany与非唯一键 [英] Hibernate relation OneToMany with non unique key

查看:119
本文介绍了休眠关系OneToMany与非唯一键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法描述我的问题,我再次尝试一下:

我有两个实体(表格):部门。这两个表都有 字段,不是唯一的



如何定义这些表之间的manyToMany 双向关系


  1. 合伙人拥有集合人员,并返回具有Person.CODE eq Department.CODE
  2. 的所有实体
  3. 有收集部门,返回所有实体与Department.CODE eq Partner.CODE

我需要关系定义 - 没有sql或hpql查询。

---------原始问题-------



我需要在Department和Person之间创建一对多的Hibernate关系(一个部门有很多人)。部门和人员有时间有效性(validFrom,validTill)

  class Department {
Long id;
字符串代码;
字符串名称;
日期validFrom;
日期validTill;
@OneToMany(fetch = FetchType.LAZY,mappedBy =departmentId)
@OnDelete(action = OnDeleteAction.CASCADE)
private Set< Person> persons = new HashSet< Person>();
}



  class Person {
Long id;
字符串名称;
字符串姓氏;
日期validFrom;
日期validTill;





$ b $没有ORM(休眠)很容易在特定日期选择特定部门的人员:

 从P人员选择P. *,部门d 
其中d。代码=?和
p.department_id = d.department_id和
?在d.validFrom和d.validTill和
之间?在p.validFrom和p.validTill之间

关系必须使用非唯一键(CODE )而不是部门ID



是否有可能用hibernate做类似的事情?

我不需要分开的对象并自己构造查询。



我想使用ORM提供的所有功能(延迟加载,cascase persist ...)

解决方案

已更新 首先,你提到你想要使用ORM提供的所有功能。如果是这种情况,那么你需要使用Hibernate友好的模式。 JPA持久性API和特定于Hibernate的API都提供了注释,使您可以更轻松地使用传统数据库。但是如果你想完全正确地使用Hibernate,那么你必须根据Hibernate的期望来设计你的模式。



在这种情况下,你可以通过使用一个连接公式,而不是您的关系的连接列。连接公式是一个有效的SQL片段(这可能会降低可移植性)。我在下面的例子中留下了实际的SQL。

  public class Person {
...
@OneToMany
@JoinFormula(sql片段)
私人列表< Department>部门;
...
}

public class Department {
...
@OneToMany
@JoinFormula(sql fragment)
私人列表< Person>人;
...
}

您还应该考虑将代码视为Hibernate object:

  @Embeddable 
公共类代码{
...
@Column( nullable = false,长度= 20)
私有字符串代码;
...
}

而不仅仅是一个字符串,的实体关系中的代码更有效地由Hibernate持久化上下文和行映射过程进行管理。最后,考虑实际上将Person和Department之间的关系映射为连接表格在你的模式中。然后,您可以利用 @ManyToMany @JoinTable 注释来执行实际的基于模式的关系在您的实体。


I am not able to describe my problem, I try it again with example:

I have two entities (tables): Department and Person. Both tables have a field CODE which is not unique.

How can I define manyToMany bidirectional relations between these tables?

  1. Departmen has collection Persons which returns all entities with Person.CODE eq Department.CODE
  2. Partner has collection Departments which returns all entities with Department.CODE eq Partner.CODE

I need the relation definition - no sql or hpql query.

--------- Original question -------

I need to create hibernate relation one to many between Department and Person (one Department has many persons). Department and persons has time validity (validFrom, validTill)

class Department {
  Long id;
  String code; 
  String name;
  Date validFrom;
  Date validTill;
  @OneToMany(fetch = FetchType.LAZY, mappedBy = "departmentId")
  @OnDelete(action = OnDeleteAction.CASCADE)
  private Set<Person> persons = new HashSet<Person>();
}


 class Person {
      Long id;
      String name;
      String surname;
      Date validFrom;
      Date validTill;
    }


Without ORM (hibernate) it is easy to select persons of particular department at specified date:

select P.* from Person P, Deparment d 
where d.code = ? and 
p.department_id = d.department_id and 
? between d.validFrom and d.validTill and 
? between p.validFrom and p.validTill

The relation has to use non unique key (CODE) instead of department ID.

Is it possible to do something similar with hibernate?

I don't need separated objects and construct queries myself.

I want use all feature which ORM offers (lazy loading, cascase persist ...)

解决方案

UPDATED

First off, you mention that you want to use all features which ORM offers. If this is the case then you need to use a schema that Hibernate is friendly toward. There are annotations available in both the JPA persistence API and with Hibernate-specific API that allow you to more easily work with 'legacy' databases. But if you want full and correct Hibernate use, then you have to design your schema according to what Hibernate expects.

You can do what you are asking in this case by using a join formula instead of a join column for your relationships. The join formula is a valid SQL fragment (which may reduce portability). I've left the actual SQL out in the example below.

public class Person {
    ...
    @OneToMany
    @JoinFormula("sql fragment")
    private List<Department> departments;
    ...
}

public class Department {
    ...
    @OneToMany
    @JoinFormula("sql fragment")
    private List<Person> people;
    ...
}

You should also consider treating code as a Hibernate object:

@Embeddable
public class Code {
    ...
    @Column(nullable = false, length = 20)
    private String code;
    ...
}

Rather than just a string so that the use of a code in entity relationships is more effectively managed by the Hibernate persistence context and the row mapping process.

Lastly, consider actually mapping the relationship between Person and Department as a join table in your schema. You'd then be able to take advantage of the @ManyToMany and @JoinTable annotations to do actual schema-based relationships in your entities.

这篇关于休眠关系OneToMany与非唯一键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆