【问题标题】:OneToMany unidirectional with composite pk on the Many sideOneToMany 单向与多端复合 pk
【发布时间】:2013-02-13 15:41:13
【问题描述】:

我有以下简单的例子:

@Entity
public class Profile {
 @Id
 private long id;

 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY) 
 @JoinColumn(name="profileId", nullable = false)
 private List<Preference> preferences;
}

@Entity
@IdClass(PreferenceId.class)
public class Preference1 {
  @Id
  private long id;
  @Id
  @Column(insertable = false, updatable = false, nullable = false)
  private long profileId;   
}

每当我尝试持久化配置文件时,都会有两个插入语句:

插入配置文件(id)值(?)——完美

插入 Preference(profileId, id) 值 (?, ?) -- 也很完美

然后

16:21:12,257 TRACE BasicBinder:83 - binding parameter [1] as [BIGINT] - 1
16:21:12,257 TRACE BasicBinder:83 - binding parameter [2] as [BIGINT] - 10
16:21:12,257 TRACE BasicBinder:83 - binding parameter [3] as [BIGINT] - 0
16:21:12,257 ERROR SqlExceptionHelper:144 - Invalid column index

为什么是三个参数而不是两个?

【问题讨论】:

  • 你能发布类 PreferenceId 的代码吗?也许它与您在实体中用@Id 标记的字段不匹配...

标签: hibernate jpa-2.0 hibernate-mapping


【解决方案1】:
public class PreferenceId implements Serializable {
    private long id;
    private long profileId;
    public PreferenceId(){}

    public long getId(){return id;}
    public void setId(long id){this.id = id;}
    public long getProfileId() {return profileId;}
    public void setProfileId(long profileId) {this.profileId = profileId;}
    //hashCode, equals
}

【讨论】:

    猜你喜欢
    • 2011-02-13
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    相关资源
    最近更新 更多