【问题标题】:JPA Hibernate - How to save two objects from two entities that are connectedJPA Hibernate - 如何从连接的两个实体中保存两个对象
【发布时间】:2018-01-17 03:38:59
【问题描述】:

我有两个实体:帐户和个人资料。它们以一对一的关系联系在一起。

账户实体:

@Entity
@Table(name = "account")
public class Account {

    @Id
    @Column(name = "account_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne
    private Profile profile;

    ...

}

个人资料实体:

@Entity
@Table(name = "profile")
public class Profile {

    @Id
    @Column(name = "profile_id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @OneToOne(mappedBy = "profile", cascade = CascadeType.ALL)
    private Account account;

    ...

}

问题是当我尝试在数据库中保存一个来自 Account 的新对象和一个来自 Profile 的新对象并连接它们时。像这样的:

Account account = new Account();
Profile profile = new Profile();
profile.setAccount(account);
account.setProfile(profile);

accountRepository.save(account);
profileRepository.save(profile);

这当然行不通。在寻找解决方案后,我发现我必须使用持久方法和事务。但是我还没有找到如何使用它们。我尝试使用EntityManager并创建一个persistence.xml文件,但是spring没有找到它(我把它放在目录:src/main/resources/Meta-INF)。

我的问题是:有没有更简单的方法来保存这两个对象(无需创建新的 xml 文件等)?如果没有我必须做什么才能让它发挥作用?

我在带有 Maven 的 Netbeans 中使用 spring 和 hibernate 和 mysql。

【问题讨论】:

    标签: spring hibernate maven netbeans spring-data-jpa


    【解决方案1】:

    我终于解决了! 问题是 CascadeType.All 属性在错误的位置。它应该在帐户实体中。此外,不需要profileRepository.save(profile),因为帐户的级联管理配置文件的保存。当我尝试显示(使用 JSON)一个永远递归的帐户时,我也遇到了一些问题,感谢this stackoverflow answer@JsonManagedReference@JsonBackReference),我解决了这个问题。

    所以现在我的代码如下所示:

    账户实体:

    @Entity
    @Table(name = "account")
    public class Account {
    
        @Id
        @Column(name = "account_id")
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        @OneToOne(cascade = CascadeType.ALL)
        @JoinColumn(name="profile_id")
        @JsonManagedReference
        private Profile profile;
    
        ...
    
    }
    

    个人资料实体:

    @Entity
    @Table(name = "profile")
    public class Profile {
    
        @Id
        @Column(name = "profile_id")
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        @OneToOne(mappedBy="profile")
        @JsonBackReference
        private Account account;
    
        ...
    
    }
    

    对于保存在数据库中:

    Account account = new Account();
    Profile profile = new Profile();
    profile.setAccount(account);
    account.setProfile(profile);
    
    accountRepository.save(account);
    

    【讨论】:

      【解决方案2】:

      您应该更改保存顺序。不能在配置文件之前创建帐户实体。

      Account account = new Account();
      Profile profile = new Profile();
      profile.setAccount(account);
      account.setProfile(profile);
      
      profileRepository.save(profile);
      accountRepository.save(account);
      

      【讨论】:

      • 可以,但是不能在 Account 之前创建 Profile 实体。他们都有另一个实体。
      猜你喜欢
      • 2012-08-03
      • 1970-01-01
      • 2017-10-10
      • 2016-12-14
      • 1970-01-01
      • 2012-12-19
      • 2013-11-27
      • 2014-09-30
      • 2016-02-19
      相关资源
      最近更新 更多