无法转换实体中具有@IdClass的请求元素

2022-09-03 12:41:57

我有以下设置:

@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 Data 存储库: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

你知道我做错了什么吗?谢谢!


答案 1

只是你的代码不符合JPA。问题的原因是您用作 PK 的一部分。PK 只能由以下 Java 类型的字段组成Member

  1. 基元 : 布尔值 , 字节 , 字符 , 整数 , 长整型 , 短整型
  2. java.lang : 布尔值 , 字节 , 字符 , 整数 , 长整型 , 短整型 , 字符串 , 枚举 , 字符串缓冲区
  3. java.math : BigInteger java.sql : 日期 , 时间 , 时间戳
  4. java.util : 日期 , 货币, 区域设置, 时区, UUID
  5. java.net : URI, URL
  6. javax.jdo.spi : PersistenceAbility

这应该有效:

@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
}

答案 2

你可以尝试使用你的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
}

成员存储库类

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);
}

推荐