【问题标题】:Failed to convert property value of type 'java.lang.Integer' to required type无法将类型“java.lang.Integer”的属性值转换为所需类型
【发布时间】:2019-04-25 17:51:07
【问题描述】:

我正在设置多对多关系,我的联接表 JoinTable 有一个带有两个键的复合 Pk,问题是当我尝试保持我的实体休眠时,他一直说他无法将 java.lang.Long 转换为所需类型(UserWeeks

我已尝试将属性更改为 java 原语(分别为 long 和 int),但 hibernate 无法将此值设置为目标类型(对象)。

我已经像这样映射关系:

User.java

@Column(name="id", nullable=false)  
@Id 
@GeneratedValue(generator="USER_ID_GENERATOR")  
@org.hibernate.annotations.GenericGenerator(name="USER_ID_GENERATOR", strategy="native")    
private long id;

@OneToMany(mappedBy="user", targetEntity=com.ti.project.Model.JoinTable.class)  @org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK}) 
@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)  
private java.util.Set jointable = new java.util.HashSet();

Weeks.java

@Column(name="week_id", nullable=false) 
@Id 
@GeneratedValue(generator="WEEK_ID_GENERATOR")  
@org.hibernate.annotations.GenericGenerator(name="WEEK_ID_GENERATOR", strategy="native")    
private int week_id;

@OneToMany(mappedBy="week",targetEntity=com.ti.project.Model.JoinTable.class)   
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.LOCK}) 
@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.TRUE)  
private java.util.Set jointable = new java.util.HashSet();

JoinTable.java

@PrimaryKeyJoinColumn       
@ManyToOne(targetEntity=com.ti.project.Model.User.class, fetch=FetchType.LAZY)  
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.LOCK})    
@JoinColumns(value={ @JoinColumn(name="user_id", referencedColumnName="id", nullable=false) }, foreignKey=@ForeignKey(name="FKuser_su465470"))  
private com.ti.project.Model.User user;

@Column(name="user_id", nullable=false, insertable=false, updatable=false)  
@Id     
@org.hibernate.annotations.GenericGenerator(name="USERID_GENERATOR", strategy="foreign", parameters=@org.hibernate.annotations.Parameter(name="property", value="user"))    
private long userId;

@PrimaryKeyJoinColumn   
@ManyToOne(targetEntity=com.ti.project.Model.Weeks.class, fetch=FetchType.LAZY) 
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.LOCK})    
@JoinColumns(value={ @JoinColumn(name="week_id", referencedColumnName="week_id", nullable=false) }, foreignKey=@ForeignKey(name="FKweek_su338979")) 
private com.ti.project.Model.Weeks week;

@Column(name="week_id", nullable=false, insertable=false, updatable=false)  
@Id 
@GeneratedValue(generator="WEEKID_GENERATOR")   
@org.hibernate.annotations.GenericGenerator(name="WEEKID_GENERATOR", strategy="foreign", parameters=@org.hibernate.annotations.Parameter(name="property", value="week"))    
private int weekId;

JoinTablePK.java

@ManyToOne(targetEntity=com.ti.project.Model.User.class, fetch=FetchType.LAZY)  
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.LOCK})    
@JoinColumns(value={ @JoinColumn(name="user_id", referencedColumnName="id", nullable=false) }, foreignKey=@ForeignKey(name="FKuser_su465470"))  
private com.ti.project.Model.User user;

@ManyToOne(targetEntity=com.ti.project.Model.Weeks.class, fetch=FetchType.LAZY) 
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.LOCK})    
@JoinColumns(value={ @JoinColumn(name="week_id", referencedColumnName="week_id", nullable=false) }, foreignKey=@ForeignKey(name="FKweek_su338979")) 
private com.ti.project.Model.Weeks week;

这是生成的异常:

Failed to convert property value of type 'java.lang.Integer' to required type 'com.ti.project.Model.Weeks' for property 'week'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.Integer' to required type 'com.ti.project.Model.Weeks' for property 'week': no matching editors or conversion strategy found

我希望保留这个JoinTable 实体,但这个例外让我抓狂有什么想法吗?

【问题讨论】:

    标签: java spring hibernate spring-boot jpa


    【解决方案1】:

    你的映射全错了。 您有 @OneToMany 和 @ManyToOne 映射。你为什么使用中间表?您只需要 @ManyToMany 关系中的中间表。 删除 JoinTable.class,因为在您的代码中没有引用。

    另一个问题是你的targetClass。 了解如何映射 @OneToMany 和 @ManyToOne 关系。一定很简单。

    https://www.baeldung.com/hibernate-one-to-many

    祝你好运!

    【讨论】:

    • 嗨蒂亚戈,感谢您的回复。实际上这是一个@ManyToMany,这就是JoinTable中的中间表,用户和周是复合PK的原因,也是来自用户和周表的FK。
    猜你喜欢
    • 2017-04-21
    • 2017-04-24
    • 1970-01-01
    • 2015-01-22
    • 2021-06-13
    • 2021-07-08
    • 2018-06-10
    • 2017-02-23
    • 2017-05-12
    相关资源
    最近更新 更多