简体   繁体   English

有限制的休眠实体

[英]Hibernate entity with restriction

We have a DB table that is mapped into a hibernate entity. 我们有一个映射到休眠实体的数据库表。 So far everything goes well... 到目前为止一切顺利...

However what we want is to only map enentitys that satisty a specific criteria, like ' distinct(fieldA,fieldB) '... 但是我们想要的是仅映射满足特定条件的实体,例如'distinct(fieldA,fieldB)'...

Is it possible to map with hibernate and hibernate annotations? 是否可以使用休眠和休眠注释进行映射? How can we do it? 我们该怎么做? With @Filter? 使用@Filter吗?

I would recommend that you use @Where annotation. 我建议您使用@Where批注。 This annotation can be used on the element Entity or target entity of a collection. 可以在集合的元素实体或目标实体上使用此注释。 You provide a clause attribute written in sql that will be applied to any select that hibernate performs on that entity. 您提供了一个用sql编写的子句属性,该属性将应用于hibernate在该实体上执行的所有选择。 It is very easy to use and very readable. 它非常易于使用并且可读性强。

Here is an example. 这是一个例子。

@Entity
//I am only interested in Donuts that have NOT been eaten
@Where(clause = "EATEN_YN = 'N'")
public class Donut {

  @Column(name = "FILLING")
  private String filling;

  @Column(name = "GLAZED")
  private boolean glazed = true;

  @Column(name = "EATEN_YN")
  private boolean eaten = false;

...

}
  1. You could create a view and then map that view to entity: 您可以创建一个视图,然后将该视图映射到实体:

     create view my_data as select ... from ... @Entity(table="my_data") public class MyData { ... } 
  2. One option is to map the table normally, then you could fetch your always entities through a query or a filter . 一种选择是正常映射表,然后您可以通过查询或过滤器来获取始终实体。

  3. You could also make a native SQL query and map the entity on the results: 您还可以进行本机SQL查询 ,并将实体映射到结果:

     Query q = sess.createSQLQuery("SELECT DISTINCT fieldA, fieldB FROM some_table") .addEntity(MyEntity.class); List<MyEntity> cats = q.list(); 
  4. It might be also possible to add DISTINCT to this type of HQL query : 也可以将DISTINCT添加到这种类型的HQL查询中

     select new Family(mother, mate, offspr) from DomesticCat as mother join mother.mate as mate left join mother.kittens as offspr 

Methods 1, 3 and 4 will make a read-only mapping. 方法1、3和4将进行只读映射。

Could you be more specific about the criteria you are using? 您能否更详细地说明所使用的标准? The view approach is more generic since you can't do everything with a hibernate query or filter. 视图方法更为通用,因为您无法使用休眠查询或过滤器来完成所有操作。

perhaps you could create a new Pojo that encapsulates the fields and the condition that they should statisy . 也许您可以创建一个新的Pojo,其中封装了这些字段及其应进行统计的条件。 And then then make that class a 'custom user defined type', such that Hibernate will have to use the mapping class that you provide, for mapping that 'type'.. 然后,将该类设置为“自定义用户定义的类型”,以便Hibernate必须使用您提供的映射类来映射该“类型”。

In addition to the options mentioned by Juha, you can also create an object directly out of a SQL query using the NamedNativeQuery and SqlResultSetMapping annotations. 除了Juha提到的选项之外,您还可以使用NamedNativeQuery和SqlResultSetMapping批注直接从SQL查询中创建对象。

@Entity
@SqlResultSetMapping(name = "compositekey", entities = 
  @EntityResult(entityClass = MiniBar.class, 
    fields = { @FieldResult(name = "miniBar", column = "BAR_ID"), })
)
@NamedNativeQuery(name = "compositekey", 
   query = "select BAR_ID from BAR", resultSetMapping = "compositekey")
@Table(name = "BAR")
public class Bar {

Flavor the SQL query to your taste 根据您的喜好添加SQL查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM