我有这个相关的实体
Entity.java
@Entity
@Table(name = "entity")
public class Entidad extends BaseEntity {
/**
* Serial
*/
private static final long serialVersionUID = 3453805453717178734L;
protected String name;
protected String image;
protected String notes;
@OrderBy("id")
@ManyToMany
protected Set<EntityHeader> headersType = new HashSet<EntityHeader>();
//Getters and setters
}
HeaderType.java
@Entity
@Table(name = "header_type")
public class HeaderType extends BaseEntity {
/**
* Serial
*/
private static final long serialVersionUID = -7471230510640592406L;
private String name;
@OrderBy("id")
@ManyToMany
private Set<EntityHeader> entities = new HashSet<EntityHeader>();
//Getters and setters
}
中间表 EntityHeader.java
@Entity
@Table(name = "entity_header")
public class EntityHeader implements Serializable{
/**
* Serial
*/
private static final long serialVersionUID = 1710372870796468955L;
@Id
@ManyToOne
private Entidad entity;
@Id
@ManyToOne
private HeaderType headerType;
private String value;
//Getters and setters
}
因此,当我想要保存没有EntityHeader数据的实体时,它表现不错,但是,如果我尝试设置EntityHeader字段并保存它(保存人员或单独保存EntityHeader,其中Entity和HeadersType具有正确的值),我收到此消息:
org.hibernate.TypeMismatchException:提供了错误类型的id class es.paquete.basicas.entidades.intermedias.EntityHeader。预期: class es.paquete.basicas.entidades.intermedias.EntityHeader,得到了课程 java.lang.Long中
编辑:主要类的ID在BaseEntity中
答案 0 :(得分:3)
您在EntityHeader上的主键没有意义:
@Id注释
指定实体的主键。该领域或财产 应用Id注释应该是以下类型之一:any Java原始类型;任何原始包装类型;串; java.util.Date; java.sql.Date; java.math.BigDecimal的; java.math.BigInteger中。
查看http://www.objectdb.com/api/java/jpa/ManyToOne
您需要查看实体的主键,以便它们可以互相引用。
答案 1 :(得分:2)
在我看来,有一些事情需要修复才能使这些实体发挥作用:
我认为你的课程应该是这样的:
<强> Entidad.java 强>
@Entity
@Table(name = "entity")
public class Entidad extends BaseEntity {
/**
* Serial
*/
private static final long serialVersionUID = 3453805453717178734L;
@Id
//@GeneratedValue(strategy= GenerationType.TABLE) -- this annotations enables automatic generation for table ids
protected Long id;
protected String name;
protected String image;
protected String notes;
@OrderBy("id")
@OneToMany(mappedBy = "entity")
protected Set<EntityHeader> headersType = new HashSet<EntityHeader>();
//Getters and setters
}
<强> HeaderType.java 强>
@Entity
@Table(name = "header_type")
public class HeaderType extends AbstractBasicEntity {
/**
* Serial
*/
private static final long serialVersionUID = -7471230510640592406L;
@Id
//@GeneratedValue(strategy= GenerationType.TABLE) -- this annotations enables automatic generation for table ids
protected Long id;
private String name;
@OrderBy("id")
@OneToMany(mappedBy = "headerType")
private Set<EntityHeader> entities = new HashSet<EntityHeader>();
//Getters and setters
}
<强> EntityHeader.java 强>
@Entity
@Table(name = "entity_header")
public class EntityHeader implements Serializable{
/**
* Serial
*/
private static final long serialVersionUID = 1710372870796468955L;
@Id
private Long id;
@ManyToOne
@JoinColumn
private Entidad entity;
@ManyToOne
@JoinColumn
private HeaderType headerType;
private String value;
//Getters and setters
}
这个解决方案必须改进,但我希望它能帮助你走上正确的道路。
PS:对不起,如果我犯了英文错误。