第一次保存呼叫后,spring jpa不保存实体

时间:2017-11-30 09:31:22

标签: mysql jpa spring-boot transactions

分别将三个实体保存到表中。

@Transactional
    @Override
    public Account save(JoinVO vo) throws MessagingException{

        Account account = converter.convert(vo, Account.class);
        AccountProfile profile = converter.convert(vo, AccountProfile.class);
        account.setProfile(profile);
        profile.setAccount(account);

        AccountSecurity security = AccountSecurity.of(1, 0, 0);
        security.setActivateKey(randomGenerator.generate(255));
        account.setSecurity(security);
        security.setAccount(account);

        account = repository.save(account);
        accountProfileService.save(profile);
        accountSecurityService.save(security);

        emailContext.sendJoinApprovalMessage(account.getEmail(), account.getProfile().getNickname(), account.getSecurity().getActivateKey());

        return account;
    }

这是我的实体:

帐户实体。

@Entity
@Table(name="account", uniqueConstraints={
    @UniqueConstraint(columnNames="account_seq"),
    @UniqueConstraint(columnNames="email")
})
@DynamicInsert
@DynamicUpdate
@Data
@EqualsAndHashCode
@ToString(includeFieldNames=true)
@RequiredArgsConstructor(staticName="of")
@NoArgsConstructor
public class Account {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="account_seq", nullable=false, unique=true)
    private int accountId;

    @Column(name="email", nullable=false, unique=true)
    @NonNull
    private String email;

    @NonNull
    private String password;

    @OneToOne(cascade=CascadeType.ALL)
    @PrimaryKeyJoinColumn
    @JoinColumn(name="account_seq", referencedColumnName="account_seq")
    private AccountProfile profile;

    @OneToOne(cascade=CascadeType.ALL)
    @PrimaryKeyJoinColumn
    @JoinColumn(name="account_seq", referencedColumnName="account_seq")
    private AccountSecurity security;
}

AccountProfile实体。

@Entity
@Table(name="account_profile", uniqueConstraints={
    @UniqueConstraint(columnNames={"account_seq"}),
    @UniqueConstraint(columnNames={"nickname"})
})
@DynamicInsert
@DynamicUpdate
@Data
@EqualsAndHashCode
@RequiredArgsConstructor(staticName="of")
@NoArgsConstructor
@ToString(includeFieldNames=true)
public class AccountProfile {

    @Id
    @Column(name="account_seq", nullable=false, unique=true)
    private int accountId;

    @Column(name="nickname", nullable=false)
    @NonNull
    private String nickname;

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


}

帐户安全实体。

@Entity
@Table(name="account_security", uniqueConstraints={
        @UniqueConstraint(columnNames="account_seq")
})
@DynamicInsert
@DynamicUpdate
@Data
@RequiredArgsConstructor(staticName="of")
@NoArgsConstructor
@ToString(includeFieldNames=true)
@EqualsAndHashCode
public class AccountSecurity {


    @Id
    @Column(name="account_seq", unique=true, nullable=false)
    private int accountId;

    @NonNull
    private Integer enabled;

    @NonNull
    private Integer activated;

    @Column(name="activate_key", unique=true, nullable=false)
    private String activateKey;

    @Column(name="login_failed", nullable=false)
    @NonNull
    private Integer loginFailed;

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


}

当我运行代码时,第一次保存成功,但没有日志插入带有数据的第二个,第三个表。

我用谷歌搜索,它说我必须将@Transactional注释标记到我想更新数据库存储的位置。但仍然没有运气。我在这里想念的是什么?

此外,我正在使用Spring Boot和JPA,mysql 5.7。

CASE CLOSED。 我更改了我的实体信息,并且工作顺利。

帐户 - 已删除的PrimaryJoinKeyColumn和' mapped-by'链接所有者已更改

@Entity
@Table(name="account", uniqueConstraints={
    @UniqueConstraint(columnNames="account_seq"),
    @UniqueConstraint(columnNames="email")
})
@DynamicInsert
@DynamicUpdate
@Data
@EqualsAndHashCode
@ToString(includeFieldNames=true)
@RequiredArgsConstructor(staticName="of")
@NoArgsConstructor
public class Account implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="account_seq", nullable=false, unique=true)
    private Integer accountId;

    @Column(name="email", nullable=false, unique=true)
    @NonNull
    private String email;

    @NonNull
    private String password;

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

    @OneToOne(cascade=CascadeType.ALL, mappedBy="account")
    private AccountSecurity security;
}

AccountProfile - 已删除的PrimaryJoinKeyColumn和' mapped-by'链接所有者已更改

@Entity
@Table(name="account_profile", uniqueConstraints={
    @UniqueConstraint(columnNames={"account_seq"}),
    @UniqueConstraint(columnNames={"nickname"})
})
@DynamicInsert
@DynamicUpdate
@Data
@EqualsAndHashCode
@RequiredArgsConstructor(staticName="of")
@NoArgsConstructor
@ToString(includeFieldNames=true)
public class AccountProfile implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="account_seq", referencedColumnName="account_seq")
    private Account account;

    @Column(name="nickname", nullable=false)
    @NonNull
    private String nickname;

}

AccountSecurity -deleted PrimaryJoinKeyColumn和' mapped-by'链接所有者已更改

@Entity
@Table(name="account_security", uniqueConstraints={
        @UniqueConstraint(columnNames="account_seq")
})
@DynamicInsert
@DynamicUpdate
@Data
@RequiredArgsConstructor(staticName="of")
@NoArgsConstructor
@ToString(includeFieldNames=true)
@EqualsAndHashCode
public class AccountSecurity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @OneToOne
    @JoinColumn(name="account_seq", referencedColumnName="account_seq")
    private Account account;

    @NonNull
    private Integer enabled;

    @NonNull
    private Integer activated;

    @Column(name="activate_key", unique=true, nullable=false)
    private String activateKey;

    @Column(name="login_failed", nullable=false)
    @NonNull
    private Integer loginFailed;

}

我更改了逻辑,使用一个帐户存储库保存所有相关实体,而不是AccountProfileRepository和AccountSecurityRepository。

AccountService.save()如下:

@Transactional
    @Override
    public boolean save(JoinVO vo) throws MessagingException{

        Account account = converter.convert(vo, Account.class);

        AccountProfile profile = converter.convert(vo, AccountProfile.class);
        profile.setAccount(account);
        account.setProfile(profile);

        AccountSecurity security = AccountSecurity.of(1, 0, 0);
        security.setActivateKey(randomGenerator.generate(255));
        security.setAccount(account);
        account.setSecurity(security);

        account = repository.save(account);

        emailContext.sendJoinApprovalMessage(account.getEmail(), profile.getNickname(), security.getActivateKey());

        return (account != null);
    }

0 个答案:

没有答案