Java 由查询定义的Hibernate实体属性

Java 由查询定义的Hibernate实体属性,java,hibernate,hql,Java,Hibernate,Hql,我尝试用Java和Hibernate实现文档修改跟踪系统。文档的每个mod都保存有autor和timestamp。要获得文档的创建者,我需要获得最早的mod。这是: @Entity @Table(name="Modifications") public class Modification { String autor; Date dateTime; Document document; @ManyToOne public Document getDocument() {

我尝试用Java和Hibernate实现文档修改跟踪系统。文档的每个mod都保存有autor和timestamp。要获得文档的创建者,我需要获得最早的mod。这是:

@Entity
@Table(name="Modifications")
public class Modification
{
  String autor;
  Date dateTime;
  Document document;

  @ManyToOne
  public Document getDocument() {
     ...
  }

  // Author of this mod
  @Column(name="Autor")
  public String getAutor() {
      ...
  }

  // and other properties
}

@Entity
@Table(name="Documents")
public class Document
{
  private List<Modification> modifications;

  @OneToMany(mappedBy="document")
  public List<Modification> getModifications() {
    ...
  }

  // this property is question about. How should I implement this with hibernate? 
  //I don't want to store "autor" field in entity, 
  //it should be determined with query to modifications table. 
  // But I can't find any example of defining properties this way... 
  // So I need your advices :)
  public String getAutor() {
     ...
  }

  // other properties
}
@实体
@表(name=“修改”)
公共类修改
{
字符串自动读取器;
日期时间;
文件;
@许多酮
公共文档getDocument(){
...
}
//这个mod的作者
@列(name=“Autor”)
公共字符串getAutor(){
...
}
//及其他物业
}
@实体
@表(name=“文件”)
公共类文档
{
修改私人名单;
@OneToMany(mappedBy=“document”)
公共列表getModifications(){
...
}
//这个属性是关于的。我应该如何用hibernate实现它?
//我不想在实体中存储“autor”字段,
//它应该通过查询修改表来确定。
//但是我找不到任何这样定义属性的例子。。。
//所以我需要你的建议:)
公共字符串getAutor(){
...
}
//其他属性
}

我不会在
getAutor()
方法中实现查询,至少不会在实体中实现查询。 IMO实体不应自行执行查询

那么,为什么不在一些服务/DAO中提供一个方法,根据需要提供作者呢?
或者,您可能希望在文档中为作者创建一个临时字段,并让dao填充它。这样,您只需加载作者一次,并多次访问信息。

我猜您正试图通过注释来完成这项工作。您可以尝试在修改列表中设置@OrderBy,并让getAutor()函数检索第一个(或最后一个)实体

虽然我更喜欢托马斯的方法论,以便更好地区分这种逻辑