我设置了多对多关系,我的联接表JoinTable有一个带有两个键的复合Pk,问题是当我尝试持久保存我的实体时,冬眠总是说他不能将java.lang.Long转换为所需的类型( UserWeeks

我尝试过将属性更改为Java原语(分别为long和int),但是随后休眠无法将此值设置为目标类型(对象)。

我已经将这种关系映射为:

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", [email protected](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", [email protected](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映射。为什么要使用中间表?您只需要@ManyToMany关系中的中间表。
删除JoinTable.class,因为您的代码中未引用。

另一个问题是您的targetClass。
查看如何映射@OneToMany和@ManyToOne关系。必须很容易。

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

祝好运!

10-06 10:18