有限制的 Hibernate 实体

发布于 2024-08-09 22:03:04 字数 163 浏览 2 评论 0原文

我们有一个映射到休眠实体的数据库表。到目前为止一切都很顺利...

但是我们想要的是只映射满足特定标准的实体,例如“distinct(fieldA,fieldB)”...

是否可以使用 hibernate 和 hibernate 注释进行映射?我们怎样才能做到呢?用@Filter?

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) '...

Is it possible to map with hibernate and hibernate annotations? How can we do it? With @Filter?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟织青萝梦 2024-08-16 22:03:04

我建议您使用 @Where注解。此注释可用于集合的元素 Entity 或目标实体。您提供一个用 sql 编写的子句属性,该属性将应用于 hibernate 对该实体执行的任何选择。它非常易于使用且可读性强。

这是一个例子。

@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;

...

}

I would recommend that you use @Where annotation. 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. 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;

...

}
抱着落日 2024-08-16 22:03:04
  1. 您可以创建一个视图,然后将该视图映射到实体:

    创建视图 my_data 作为
    从...中选择...
    
    @Entity(表=“my_data”)
    公共类 MyData { ... }
    
  2. 一个选项是正常映射表,然后您可以通过查询或 过滤器

  3. 您还可以进行本机 SQL 查询< /a> 并将实体映射到结果上:

    查询 q = sess.createSQLQuery("从 some_table 中选择不同的 fieldA, fieldB")
              .addEntity(MyEntity.class);
    列表猫 = q.list();
    

  4. 也可以将 DISTINCT 添加到这种类型的HQL查询

    选择新家庭(母亲、伴侣、子女)
    来自 DomesticCat 作为母亲
    加入 mother.mate 作为 mate
    左加入 mother.kittens 作为 offspr
    

方法1、3、4将进行只读映射。

您能否更具体地说明您所使用的标准?视图方法更通用,因为您无法使用休眠查询或过滤器完成所有操作。

  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:

    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:

    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.

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.

2024-08-16 22:03:04

也许您可以创建一个新的 Pojo 来封装字段以及它们应该统计的条件。然后将该类设置为“自定义用户定义类型”,这样 Hibernate 将必须使用您提供的映射类来映射该“类型”。

perhaps you could create a new Pojo that encapsulates the fields and the condition that they should statisy . 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'..

野却迷人 2024-08-16 22:03:04

除了 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 {

根据您的喜好调整 SQL 查询

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.

@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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文