gpt4 book ai didi

java - 无法将类型 'java.lang.Integer' 的属性值转换为所需类型

转载 作者:行者123 更新时间:2023-12-02 06:17:53 26 4
gpt4 key购买 nike

我正在设置多对多关系,我的连接表JoinTable有一个带有两个键的复合Pk,问题是当我尝试持久化我的实体时,hibernate一直说他无法将java.lang. 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实体,但是这个异常让我发疯有什么想法吗?

最佳答案

你的映射完全错误。您有 @OneToMany 和 @ManyToOne 映射。为什么要用中间的 table ?您只需要 @ManyToMany 关系中的一个中间表。删除 JoinTable.class,因为代码中未引用该类。

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

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

祝你好运!

关于java - 无法将类型 'java.lang.Integer' 的属性值转换为所需类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55855053/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com