2016-08-27 56 views
3

我有以下設置:未能請求元素轉換成實體@IdClass

@Entity 
@IdClass(MemberAttributePk.class) 
public class MemberAttribute { 
    @Id 
    @ManyToOne @JoinColumn(name="member_id") 
    protected Member member; 

    @Id 
    protected String name; 

    private String value; 

    public MemberAttribute() {} 

    // get & set 
} 

和ID類:

public class MemberAttributePk implements Serializable { 
    protected Member member; 
    protected String name; 

    public MemberAttributePk() {} 

    // get & set 
} 

我已經定義了一個簡單的Spring數據存儲庫MemberAttribute

@Repository 
public interface MemberAttributeRepo extends JpaRepository<MemberAttribute, MemberAttributePk> { 
} 

現在,我想要做的就是將成員屬性保存到數據庫中:

public void saveAttribute(Member member, String name, String value) { 
    MemberAttribute attr = new MemberAttribute(member, name, value); 
    attributeRepo.save(attr); 
} 

不過,我結束了與此服務器例外:

2016-08-28 00:24:20.673 WARN 5656 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver : 
Failed to convert request element: org.springframework.beans.ConversionNotSupportedException: 
Failed to convert property value of type [java.lang.Long] to required type [com.example.Member] for property 'member'; nested exception is java.lang.IllegalStateException: 
Cannot convert value of type [java.lang.Long] to required type [com.example.Member] for property 'member': 
no matching editors or conversion strategy found 

任何想法,我究竟做錯了什麼? 謝謝!

回答

5

只是您的代碼不符合JPA。造成問題的原因是您使用Member作爲PK的一部分。 的PK只能由以下Java類型

  1. 基元場區的布爾,字節,CHAR,INT,長,短
  2. java.lang中:布爾字節,字符,整數,長,短,字符串,枚舉,StringBuffer
  3. java.math:BigInteger java.sql:Date,Time,Timestamp
  4. java.util中:日期,貨幣,地點,時區UUID
  5. java.net:URI,URL
  6. javax.jdo。SPI:PersistenceCapable

這應該工作:

@Embeddable 
public class MemberAttributePk implements Serializable { 
    @Column(name = "member_id") 
    protected Long memberId; 
    @Column(name = "name") 
    protected String name; 

    public MemberAttributePk() {} 

    // get & set 
} 

@Entity 
public class MemberAttribute { 

    @EmbeddedId 
    protected MemberAttributePk memberAttributePk; 

    @ManyToOne 
    @JoinColumn(name="member_id") 
    protected Member member; 

    private String value; 

    public MemberAttribute() {} 

    // get & set 
} 

或用@ClassId

public class MemberAttributePk implements Serializable { 
    protected Long memberId; 
    protected String name; 

    public MemberAttributePk() {} 

    // get & set 
} 

@Entity 
@IdClass(MemberAttributePk.class) 
public class MemberAttribute { 
    @Id 
    @Column(name = "member_id") 
    protected Long memberId; 

    @Id 
    @Column(name = "name") 
    protected String name; 

    @ManyToOne 
    @JoinColumn(name="member_id") 
    protected Member member; 

    private String value; 

    public MemberAttribute() {} 

    // get & set 
} 
+0

謝謝!而已。小成員提到'Member'應該被映射爲:'@ManyToOne @JoinColumn(name =「member_id」,可插入= false,可更新= false)' – Ionut

1

一樣,你可以嘗試使用您的MemberRepository保存它,因爲我相信您的會員類和MemberAttribute類擁有一對多的rel ationship參考,下面這裏是例子

會員類

@Entity 
public class Member { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    public long id; 

    @OneToMany(mappedBy = "Member", cascade = CascadeType.ALL) 
    private Set<MemberAttribute> mMemberAttributes = new HashSet<>(); 

    public void setMemberAttributes(Set<MemberAttribute> mMemberAttributes){ 
     this.mMemberAttributes = mMemberAttributes; 
    } 

    public Set<MemberAttribute> getMemberAttributes(){ 
     return mMemberAttributes; 
    } 

    // other code 
} 

MemberRepository類

public interface MemberRepository extends JpaRepository<Member, Long> { 

} 

代碼中的保存功能

public void saveAttribute(Member member, String name, String value) { 
    MemberAttribute attr = new MemberAttribute(member, name, value); 
    member.getMemberAttributes().add(attr); 
    memberRepository.save(member); 
}