Failed to convert request element in entity with @IdClass(无法使用 @IdClass 转换实体中的请求元素)
问题描述
我有以下设置:
@Entity@IdClass(MemberAttributePk.class)公共类成员属性 {@ID@ManyToOne @JoinColumn(name="member_id")受保护的会员;@ID受保护的字符串名称;私有字符串值;公共成员属性(){}//获取 &放}
还有id类:
public class MemberAttributePk 实现 Serializable {受保护的会员;受保护的字符串名称;公共 MemberAttributePk() {}//获取 &放}
我已经为 MemberAttribute
定义了一个简单的 Spring Data 存储库:
@Repository公共接口 MemberAttributeRepo 扩展 JpaRepository<MemberAttribute, MemberAttributePk>{}
现在,我要做的就是将成员属性持久化到数据库中:
public void saveAttribute(Member member, String name, String value) {MemberAttribute attr = new MemberAttribute(member, name, value);属性Repo.save(attr);}
但是,我最终得到了这个服务器异常:
2016-08-28 00:24:20.673 WARN 5656 --- [nio-8080-exec-8] .w.s.m.s.DefaultHandlerExceptionResolver :无法转换请求元素:org.springframework.beans.ConversionNotSupportedException:无法将类型 [java.lang.Long] 的属性值转换为属性成员"所需的类型 [com.example.Member];嵌套异常是 java.lang.IllegalStateException:无法将类型 [java.lang.Long] 的值转换为属性成员"所需的类型 [com.example.Member]:未找到匹配的编辑器或转换策略
知道我做错了什么吗?谢谢!
只是你的代码不符合 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:日期、货币、语言环境、时区、UUID
- java.net:URI、URL
- javax.jdo.spi:PersistenceCapable
<小时>
这应该可行:
@Embeddable公共类 MemberAttributePk 实现 Serializable {@Column(name = "member_id")受保护的长成员 ID;@Column(名称 = "名称")受保护的字符串名称;公共 MemberAttributePk() {}//获取 &放}@实体公共类成员属性 {@EmbeddedId受保护的成员属性库成员属性库;@ManyToOne@JoinColumn(name="member_id")受保护的会员;私有字符串值;公共成员属性(){}//获取 &放}
或与 @ClassId
public class MemberAttributePk 实现 Serializable {受保护的长成员 ID;受保护的字符串名称;公共 MemberAttributePk() {}//获取 &放}@实体@IdClass(MemberAttributePk.class)公共类成员属性 {@ID@Column(name = "member_id")受保护的长成员 ID;@ID@Column(名称 = "名称")受保护的字符串名称;@ManyToOne@JoinColumn(name="member_id")受保护的会员;私有字符串值;公共成员属性(){}//获取 &放}
I have the following setup:
@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
}
And the id class:
public class MemberAttributePk implements Serializable {
protected Member member;
protected String name;
public MemberAttributePk() {}
// get & set
}
I have defined a simple Spring Data repository for MemberAttribute
:
@Repository
public interface MemberAttributeRepo extends JpaRepository<MemberAttribute, MemberAttributePk> {
}
Now, all I want to do is persist a member attribute to the database:
public void saveAttribute(Member member, String name, String value) {
MemberAttribute attr = new MemberAttribute(member, name, value);
attributeRepo.save(attr);
}
However, I end up with this server exception:
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
Any idea what am I doing wrong? Thanks!
Simply your code is not JPA compliant. The cause of problem is that you use Member
as a part of your PK.
The PK can only be made up of fields of the following Java types
- Primitives : boolean , byte , char , int , long , short
- java.lang : Boolean , Byte , Character , Integer , Long , Short , String , Enum , StringBuffer
- java.math : BigInteger java.sql : Date , Time , Timestamp
- java.util : Date , Currency, Locale, TimeZone, UUID
- java.net : URI, URL
- javax.jdo.spi : PersistenceCapable
This should work:
@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
}
Or the same with @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
}
这篇关于无法使用 @IdClass 转换实体中的请求元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法使用 @IdClass 转换实体中的请求元素
基础教程推荐
- Java:带有char数组的println给出乱码 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01