我正在设置多对多关系,我的联接表JoinTable具有带有两个键的复合Pk,问题是当我尝试保持实体休眠状态时,我总是说他无法转换java.lang.Long
设置为所需的类型(User
或Weeks
)
我尝试将属性更改为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", 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
实体,但是这种例外使我发疯了,知道吗?
答案 0 :(得分:0)
您的映射都是错误的。 您具有@OneToMany和@ManyToOne映射。为什么使用中间表?您只需要@ManyToMany关系中的中间表。 删除JoinTable.class,因为您的代码中未引用它。
另一个问题是您的targetClass。 查看如何映射@OneToMany和@ManyToOne关系。一定要容易。
https://www.baeldung.com/hibernate-one-to-many
祝你好运!