Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jpa 单向多对一:保存父实体会在子表中生成不需要的插入_Jpa_Many To One_Composite Primary Key - Fatal编程技术网

Jpa 单向多对一:保存父实体会在子表中生成不需要的插入

Jpa 单向多对一:保存父实体会在子表中生成不需要的插入,jpa,many-to-one,composite-primary-key,Jpa,Many To One,Composite Primary Key,我需要使用@JoinColumn@ManyToOne列作为复合主键的一部分(具有用于定义复合键@IdClass而不是@EmbeddedId的首选选项) 所涉及的enities是父和子关系,类似于employee和department,唯一的区别是我只需要从父('employee')访问子('department')字段,而不需要从父('employee')访问子('department')字段。因此,我在父实体中使用单向@ManyToOne 目前,我在更新父表时遇到了一个问题—在更新父实体时,h

我需要使用@JoinColumn@ManyToOne列作为复合主键的一部分(具有用于定义复合键@IdClass而不是@EmbeddedId的首选选项)

所涉及的enities是父和子关系,类似于employee和department,唯一的区别是我只需要从父('employee')访问子('department')字段,而不需要从父('employee')访问子('department')字段。因此,我在父实体中使用单向@ManyToOne

目前,我在更新父表时遇到了一个问题—在更新父实体时,hibernate尝试将一条记录插入子表,但由于主键冲突而失败,这是可以理解的。我需要抑制对子表的插入。我希望注释中的'insertable=false,updateable=false'能够解决这个问题,但事实并非如此

我使用这些指南设置复合主键:

我的代码如下。 母公司:

@Data
@Builder
@Entity
@Table(name = "Parent", schema = "dbo")
@IdClass(value = ParentPrimaryKey.class)
public class Parent {


    @Id
    @Column(name = "dateTime", insertable = false, updatable = false)
    private Timestamp dateTime;

    @Id
    @MapsId("child")
    @ManyToOne
    @JoinColumn(name = "child", insertable = false, updatable = false)
    private Child child;

    @Column(name = "status")
    private Integer status;

}
父级的主键实体:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ParentPrimaryKey implements Serializable {

    private static final long serialVersionUID = 1;

    private Timestamp dateTime;
    private String child;

}
子实体:

@Data
@Entity(name = "Child")
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "Child", schema = "dbo")
public class Location implements Serializable {

    private static final long serialVersionUID = 1;

    @Id
    @Column(name = "child")
    private String child;
    @Column(name = "param")
    private String param;
    // some other child param fields

}
在内存H2上进行集成测试,测试表中的一些条目可用:

    @Test
    public void shouldSaveRecord() {
        Parent selectedRecord = parentRepository.findAll().get(0);
        selectedRecord.setStatus(9);
        parentRepository.save(selectedRecord);
    }
运行测试生成的日志:

2019-07-01 09:52:20.547 [main] DEBUG org.hibernate.SQL - select parent0_.dateTime as dateTime1_15_, parent0_.child as child 3_15_, parent0_.status as status4_15_ from dbo.Parent parent0_
2019-07-01 09:52:20.555 [main] DEBUG org.hibernate.SQL - select child0_.child as child1_5_0_, child0_.param as param3_5_0_ from dbo.Child child0_ where child0_.child=?
2019-07-01 09:52:20.602 [main] DEBUG org.hibernate.SQL - select parent0_.dateTime as dateTime1_15_0_, parent0_.child as child3_15_0_, parent0_.status as status4_15_0_ from dbo.Parent parent0_ where parent0_.dateTime=? and parent0_.child=?
2019-07-01 09:52:20.630 [main] DEBUG org.hibernate.SQL - insert into dbo.child (child, param) values (?, ?)
2019-07-01 09:52:20.636 [main] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 23505, SQLState: 23505
2019-07-01 09:52:20.636 [main] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Unique index or primary key violation: "PRIMARY_KEY_81 ON DBO.CHILD(CHILD) VALUES ('MTS_0102_010R4', 8)"; SQL statement:
insert into dbo.child (child, param) values (?, ?) [23505-197]

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint ["PRIMARY_KEY_81 ON DBO.CHILD(CHILD) VALUES ('MTS_0102_010R4', 8)"; SQL statement:
insert into dbo.child (child, param) values (?, ?) [23505-197]]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement


我需要禁止在子表中插入不需要的内容。 任何帮助都将不胜感激

Upd 2019-07-02。当前解决方案:必须切换到@EmbeddedId。有点痛苦,因为我必须更新所有非本地查询。仍然想知道是否可以用@IdClass管理它