我有以下设置:
@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
}
我为MemberAttribute
定义了一个简单的Spring数据库:
@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
知道我做错了什么吗? 谢谢!
答案 0 :(得分:8)
只是您的代码不符合JPA。问题的原因是您使用Member
作为PK的一部分。
PK只能由以下Java类型的字段组成
- 原语:boolean,byte,char,int,long,short
- java.lang :Boolean,Byte,Character,Integer,Long,Short,String,Enum,StringBuffer
- java.math :BigInteger java.sql:日期,时间,时间戳
- java.util :日期,货币,区域设置,TimeZone,UUID
- java.net :URI,网址
- 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
}
答案 1 :(得分:1)
您可以尝试使用您的MemberRepository保存它,因为我相信您的Member类和MemberAttribute类具有一对多的关系引用,下面是示例
会员班
@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);
}