【问题标题】:Custom associations between Hibernate EntitiesHibernate 实体之间的自定义关联
【发布时间】:2013-07-05 13:17:26
【问题描述】:

我有两个实体类,它们都被写入一个单独的表中。在我的数据库中,两个表之间没有通过键连接,但我想实现实体之间的逻辑关联。

@Entity
public class One{

  @ID
  long id;

  String name;

  List<Two> aListOfTwos;;

  public List<Two> getAListOfTwos(){
    //return some Twos based on some custom query, p.e. "All Twos created last week"
  }



}



@Entity
public class Two{

  @ID
  long id;

  Date createdAt;

  String name;

}

当然,我可以在 getAListOfTwos 方法中编写一个适当的查询,但是我需要访问我不想要的会话对象。有没有其他方法可以在 Hibernate 中指定这样的关联?

非常感谢 保罗

【问题讨论】:

    标签: java hibernate associations


    【解决方案1】:

    您可以使用连接表进行一对多映射。如果你的关联是多对多的,它无论如何都需要第三张表。

    【讨论】:

    • 谢谢,但这是我实际上试图避免的。这个额外的表格如何确保我只得到“上周创建的两个”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多