无法保存@ManyToOne子实体

时间:2018-10-22 14:23:42

标签: hibernate spring-boot jpa

我试图通过提供子实体的数据以及父实体的数据来使用 JpaRepository 保存数据,但是保存操作仅对父实体而不对子实体执行。下面是我的实现:

我的孩子实体:

@Entity
@Table(name = "justchild", schema = "test")
public class JustChildEntity implements Serializable {

    private static final long serialVersionUID = 8495817802073010928L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private long id;

    @Basic
    @Column(name = "cname", nullable = true)
    private String cname;

    @ManyToOne
    @JoinColumn(name = "fk_id", referencedColumnName = "id")
    private JustParentEntity justParentEntity;

    // getters and setters
}

我的父实体:

@Entity
@Table(name = "justparent", schema = "test")
public class JustParentEntity implements Serializable {

    private static final long serialVersionUID = -748956247024967638L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private long id;

    @Basic
    @Column(name = "name", nullable = true)
    private String name;

    @OneToMany(mappedBy = "justParentEntity", targetEntity = JustChildEntity.class)
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    private Set<JustChildEntity> justChildEntities;

    // getters and setters
}

我通过控制器的实现:

@Autowired
private JustParentRepo justParentRepo;

@GetMapping("/test-persist-many")
@ResponseBody
public void testPersistMany() {

    JustParentEntity justParentEntity = new JustParentEntity();
    justParentEntity.setName("parent 1");

    JustChildEntity justChildEntity = new JustChildEntity();
    justChildEntity.setCname("child Name 1");
    justChildEntity.setJustParentEntity(justParentEntity);

    JustChildEntity justChildEntity2 = new JustChildEntity();
    justChildEntity2.setCname("child Name 2");
    justChildEntity.setJustParentEntity(justParentEntity);

    Set<JustChildEntity> justChildEntities = new HashSet<>();
    justChildEntities.add(justChildEntity);
    justChildEntities.add(justChildEntity2);

    justParentEntity.setJustChildEntities(justChildEntities);

    justParentRepo.save(justParentEntity);

    ObjectMapper objectMapper = new ObjectMapper();
    try {
        String json = objectMapper.writeValueAsString(justParentEntity);
        System.out.println(json);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
}

我的存储库界面:

@Repository
@Transactional
public interface JustParentRepo extends JpaRepository<JustParentEntity, Long> {
}

我的序列化json输出如下:

{
    "id": 8,
    "name": "parent 1",
    "justChildEntities": [{
        "id": 0,
        "cname": "child Name 1",
        "justParentEntity": null
    }, {
        "id": 0,
        "cname": "child Name 2",
        "justParentEntity": null
    }]
}

在这里,我看到子实体没有id值,也没有分配外键。我正在使用Spring Boot 1.5.13。请帮忙。

更新

保存后我添加了justParentRepo.flush(),但无法正常工作。

我的application.properties文件:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false&autoReconnect=true
spring.datasource.username=root
spring.datasource.password=12345
spring.datasource.tomcat.max-wait=20000
spring.datasource.tomcat.max-active=50
spring.datasource.tomcat.max-idle=20
spring.datasource.tomcat.min-idle=15
spring.datasource.tomcat.test-while-idle=true
spring.datasource.tomcat.test-on-borrow=true
spring.datasource.tomcat.time-between-eviction-runs-millis=3600000
spring.datasource.tomcat.validation-query=SELECT 1
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.id.new_generator_mappings=true
spring.jpa.properties.hibernate.format_sql=true

1 个答案:

答案 0 :(得分:1)

尝试一下:

@OneToMany(mappedBy = "justParentEntity", cascade = CascadeType.ALL)

如果这不起作用,可以在应用程序属性文件中指定连接设置吗?