【问题标题】:JPA2 annotations for uni-directional relationship单向关系的 JPA2 注释
【发布时间】:2011-07-06 10:13:52
【问题描述】:

你好
在下面详述的 InstrumentConfig 类中,工具和市场应该有哪些 JPA2 注释?

谢谢

@Entity
@Access(AccessType.FIELD)
class Instrument {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    long id;

    @Basic
    String code; // EURCHF, GOOG, etc.
}

@Entity
@Access(AccessType.FIELD)
class Market {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    long id;

    @Basic
    String name;
}

@Entity
@Access(AccessType.FIELD)
class InstrumentConfig {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    long id;

    // what JPA2 annotations should instrument and market have?
    Instrument instrument;
    Market market;

    @Basic
    String dataURL
}

【问题讨论】:

  • 这取决于关系以及您希望如何将其映射到数据库中。是多对一还是单对一?
  • 您好,感谢您的回复 - 这是一对一的关系。

标签: java jpa jpa-2.0


【解决方案1】:

最简单的情况就是这样(外键列默认命名,没有级联等):

@ManyToOne
Instrument instrument;     
@ManyToOne
Market market; 

【讨论】:

  • 实际上我的关系是一对一的 - 你的回答让我找到了正确的解决方案。
猜你喜欢
  • 1970-01-01
  • 2010-11-03
  • 2016-02-27
  • 2015-05-13
  • 1970-01-01
  • 1970-01-01
  • 2011-03-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多