[英]Request an embedded id
I have some problem with Hibernate. 我对Hibernate有一些问题。
I have a table withtout Id column. 我有一个没有id列的表。 So i used an embedded id to configure a couple of columns as Id. 因此,我使用嵌入式ID将几列配置为ID。 But when i query on one of these 2 Ids, I don't receive all the values. 但是,当我查询这两个ID之一时,我没有收到所有值。
The table : 桌子 :
CODE_ACTION | CODE_AVANCEMENT | DATE_AVANCEMENT | COUT
----------------------------------------------------------
A0000001 | Engagée | 09/09/11 | 2400
A0000002 | Terminée | 05/10/12 | 2700
A0000002 | Engagée | 05/10/12 | 2700
A0000003 | Terminée | 06/06/10 | 0
etc. 等等
My entity 我的实体
@Entity
@Component
@Data
@Table(name = "F_AVANCEMENT_ACTION")
public class AvancementActionDtw {
// --------------------------------------------------------
// FIELDS
// --------------------------------------------------------
@Id
private AvancementActionDtwPk id;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "DATE_AVANCEMENT", nullable = false)
private Date dateAvancement;
@Column(name = "COUT", nullable = true)
private Integer coutAction;
}
My embedded id 我的嵌入式ID
@Data
@Embeddable
@EqualsAndHashCode(of = { "codeAction", "codeAvancement" })
public class AvancementActionDtwPk implements Serializable {
private static final long serialVersionUID = 1L;
// --------------------------------------------------------
// Fields
// --------------------------------------------------------
@Column(name = "CODE_ACTION", nullable = false)
private String codeAction;
@Column(name = "CODE_AVANCEMENT", nullable = false)
private String codeAvancement;
}
EDIT : the modified embedded id 编辑:修改后的嵌入式id
@Data
@Embeddable
// @EqualsAndHashCode(of = { "codeAction", "codeAvancement" })
public class AvancementActionDtwPk implements Serializable {
private static final long serialVersionUID = 1L;
// --------------------------------------------------------
// Fields
// --------------------------------------------------------
@Column(name = "CODE_ACTION", nullable = false)
private String codeAction;
@Column(name = "CODE_AVANCEMENT", nullable = false)
private String codeAvancement;
// --------------------------------------------------------
// Constructors
// --------------------------------------------------------
public AvancementActionDtwPk() {
}
public AvancementActionDtwPk(String codeAction, String codeAvancement) {
this.codeAction = codeAction;
this.codeAvancement = codeAvancement;
}
// --------------------------------------------------------
// Equals and HashCode
// --------------------------------------------------------
@Override
public int hashCode() {
return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers
append(this.codeAction).append(this.codeAvancement).toHashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof AvancementActionDtwPk)) return false;
if (obj == this) return true;
AvancementActionDtwPk pk = (AvancementActionDtwPk) obj;
return this.codeAction.equals(pk.codeAction) && this.codeAvancement.equals(pk.codeAvancement);
}
}
The DAO DAO
@Repository
@Component
public interface AvancementActionDtwDao extends JpaRepository<AvancementActionDtw, AvancementActionDtwPk> {
@Query("from AvancementActionDtw where id.codeAction = :codeAction and id.codeAvancement = :codeAvancement")
List<AvancementActionDtw> findAvancementActionDtw(@Param("codeAction") String codeAction,
@Param("codeAvancement") String codeAvancement);
@Query("from AvancementActionDtw where id.codeAction = :codeAction")
List<AvancementActionDtw> findAvancementActionDtw(@Param("codeAction") String codeAction);
}
If i call 如果我打电话
findAvancementActionDtw("A0000002")
I only get the 2nd entry <"A0000002"; 我只得到第二个条目<“ A0000002”; "Engagée"> If I explicitly set the 2nd parameter “Engagée”>如果我明确设置了第二个参数
findAvancementActionDtw("A0000002", "Terminée")
I have no result 我没有结果
I would like to know where I'm wrong ? 我想知道我错了吗?
PS : The data are in an Oracle Database PS:数据在Oracle数据库中
From JPA 2.0 Specification, chapter 11.1.15 EmbeddedId Annotation: 根据JPA 2.0规范的第11.1.15章EmbeddedId注释:
The
EmbeddedId
annotation is applied to a persistent field or property of an entity class or mapped superclass to denote a composite primary key that is an embeddable class.EmbeddedId
注释应用于实体类或映射超类的持久字段或属性,以表示作为可嵌入类的复合主键。 The embeddable class must be annotated asEmbeddable
. 必须将embeddable类注释为Embeddable
。 Relationship mappings defined within an embedded id class are not supported. 不支持在嵌入式id类中定义的关系映射。There must be only one
EmbeddedId
annotation and noId
annotation when theEmbeddedId
annotation is used. 这里必须只有一个EmbeddedId
注释和无Id
的标注时EmbeddedId
使用注解。
I guess you would need to use @EmbeddedId to make it work: 我想您需要使用@EmbeddedId使其起作用:
@Entity
@Component
@Data
@Table(name = "F_AVANCEMENT_ACTION")
public class AvancementActionDtw {
@EmbeddedId
private AvancementActionDtwPk id;
...
}
in fact my SQL Developer was not on the same BDD that my application... 实际上,我的SQL Developer与我的应用程序不在同一BDD上。
Stupid error. 愚蠢的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.