Child 未插入

Child is not being inserted

我在使用 jpa 存储库保存 objects 时遇到困难。我有两个实体。一对一相关:

@Entity(name="USER")
public class User {
@Id
@GeneratedValue
@Column(name = "USER_ID")
private Long userId;
private String username;


@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
@LazyCollection(LazyCollectionOption.FALSE)
private Wallet wallet;

和钱包

@Entity(name = "WALLET")
public class Wallet implements Serializable {

@Id
@GeneratedValue
@Column(name = "WALLET_ID")
private Long id;

@OneToMany(mappedBy = "wallet")
@Cascade({org.hibernate.annotations.CascadeType.ALL})
@LazyCollection(LazyCollectionOption.FALSE)
private List<Paper> papers;


@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private User user;

我遇到了这种情况的问题 - 当我添加新用户时,hibernate 没有保存钱包:

public String addUser(@PathVariable("userName") String userName) {
UserDto userDto = new UserDto();
userDto.setUsername(userName);
WalletDto walletDto = new WalletDto();
userDto.setWalletDto(walletDto);
userService.addUser(userDto);
return userDto.toString();
}

和添加用户:

@Override
@Transactional
public void addUser(UserDto userDto) {
userRepository.save(userConverter.convertToEntity(userDto));
}

其中 save 是 jpa 方法。

编辑:

  public void addUser(UserDto userDto) {    
            User user = userConverter.convertToEntity(userDto);
            Wallet wallet = walletConverter.convertToEntity(userDto.getWallet());
            user.setWallet(wallet);
            userRepository.save(user);        

    }

真的很奇怪:

12:30:06,619 DEBUG EntityPrinter:121 - com.private.model.Wallet{id=6, papers=null, user=com.private.model.User#7}
12:30:06,619 DEBUG EntityPrinter:121 - com.private.model.User{username=xyzasew, userId=7, wallet=com.private.model.Wallet#6}

并且在钱包 table 中没有用户踪迹 = )

编辑。我希望快到了 =):

我希望钱包成为用户和纸张的主人,根据您的建议我编辑了一些实体:

@Entity(name = "WALLET")
public class Wallet implements Serializable {

@Id
@GeneratedValue
@Column(name = "WALLET_ID")
private Long id;

@OneToMany(mappedBy = "wallet", cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private List<Paper> papers;


@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "wallet")
private User user;

和用户:

@Entity(name="USER")
public class User {
@Id
@GeneratedValue
@Column(name = "USER_ID")
private Long userId;
private String username;


@OneToOne(cascade = CascadeType.ALL)
private Wallet wallet;

但是钱包还没有user_id,用户收到了!

mappedBy 基本上意味着我不是 relationship 的所有者,key 在另一边。将 mappedBy 放在两边表示没有人是关系的所有者。

理论上

bi-directional relationship 使用映射注释的 "mappedBy" 属性(如 @OneToOne@OneToMany@ManyToMany)。此属性允许您从两侧引用关联的实体。如果 "X" 与 "Y" 关联,那么您可以从 Y 获得 X,从 X 获得 Y。

MappedBy 向休眠发出信号,表示关系的键在另一侧。

注释@JoinColumn 表示此实体是关系的所有者。也就是说,对应的table有一列,其外键指向被引用的table,而属性mappedBy表示这一边的实体是关系的逆向关系,而所有者位于 "other" 实体中。

实用

@Entity
public class X implements Serializable {
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="y_fk")
    public Y getY() {
        ...
    }

@Entity
public class Y implements Serializable {
    @OneToOne(mappedBy = "y")
    public X getX() {
    ...
}   

因为这是用 mappedBy 注释的,所以表明这不是所有者,并且所有者是 X(注释的字段)。 name 属性告诉 Hibernate 在哪里可以找到关于 FK 映射的信息(在 X 里面有一个 getY() 方法)。不会在 Y 中创建其他字段。

以上代码待改进

@LazyCollection:

defines the lazyness option on @ManyToMany and @OneToMany associations. LazyCollectionOption can be TRUE (the collection is lazy and will be loaded when its state is accessed)

User.Java实体变化可以减少到下面

@OneToOne(cascade = CascadeType.ALL)
private Wallet wallet;