如何使用Hibernate 5和MySQL解决HHH000346错误?

时间:2019-05-22 09:45:20

标签: mysql spring hibernate web

我正在研究宁静的服务和看法。 关于这一点,我使用mysql和休眠5。 我的数据表是两个,并且具有引用关系。 当我更新主键时,会出现问题。 当我添加一个新数据然后更新另一个表(它们具有引用关系)中的现有数据时,HHH000346:托管刷新期间发生错误。

我已经在Google上进行搜索,但是找不到答案。

这是我的Entity类。

@Entity
@Table(name = "users")
@EntityListeners(AuditingEntityListener.class)
public class User {
    private long serial;
    private String username;
    private String password;

    public User() {

    }

    public User(long serial, String username, String password) {
        setSerial(serial);
        setUsername(username);
        setPassword(password);
    }

    @Column(name = "serial", nullable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    public long getSerial() {
        return serial;
    }

    public void setSerial(long serial) {
        this.serial = serial;
    }

    @Id
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Column(name = "password", nullable = false)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "serial: " + this.serial + ", username: " + this.username + ", password: " + this.password; 
    }
}
Entity
@Table(name = "sites")
@EntityListeners(AuditingEntityListener.class)
@IdClass(Site.class)
public class Site implements Serializable{
    @ManyToOne
    @JoinColumn(name="username",foreignKey=@ForeignKey(name="username"))
    private String username;
    private String siteURL;

    @Id
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Id
    public String getSiteURL() {
        return siteURL;
    }

    public void setSiteURL(String siteName) {
        this.siteURL = siteName;
    }
}

这是班上有问题。

public class UserController {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private SiteRepository siteRepository;
    private CryptoUtil passwordEncoder = new CryptoUtil();

    ...

    @PutMapping("/users/{username}")
    public User updateUser(@PathVariable(value = "username") String username, @Valid @RequestBody User userDetails)
            throws ResourceNotFoundException {
        User user = userRepository.findById(username)
                .orElseThrow(() -> new ResourceNotFoundException("User not found on :: " + username));
        List<Site> sites = siteRepository.findByUsername(user.getUsername());

        userDetails.setPassword(passwordEncoder.encryptSHA256(userDetails.getPassword()));
        final User updateUser = userRepository.save(userDetails);

        for (Site site : sites)
        {
            site.setUsername(userDetails.getUsername());
            site = siteRepository.save(site);
        }

        userRepository.delete(user);
        return updateUser;
    }

    ....
}
The for-each statement occurs error.

PLEASE HELP ME

1 个答案:

答案 0 :(得分:0)

您为什么这样做?

@ManyToOne
@JoinColumn(name="username",foreignKey=@ForeignKey(name="username"))
private String username;

应为:

@ManyToOne
@JoinColumn(name="username",foreignKey=@ForeignKey(name="username"))
private User user;

我还将建议您将主键用作外键。 而且一个实体中不能有多个@Id