休眠 - 更新非 ID 外键

hibernate - update non-id foreign key

我对休眠还很陌生,但我需要做一个一对一的映射, 我有一个 table resources 有一个外键 res_music_id 到 table music.
在我做了很多搜索和调整之后它可以工作,但仍然存在一个问题:resources 的 ID 已更新,但 res_music_id 的值未更新;所以,每次我调用 resources 的索引时,它都会在 music 中给我相同的记录。所以我也需要动态更改 res_music_id,我该怎么做?顺便说一句,我在 mySQL 中有一个数据库。 这是我的代码:

资源

package database;

import com.sun.istack.internal.NotNull;
import org.hibernate.annotations.*;
import org.hibernate.annotations.Parameter;

import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Arrays;

/**
 * @author Jari Van Melckebeke
 */
@Entity
@Table(name = "resources", catalog = "teresaDB")
public class Resources implements Serializable {

    private int resId;
    private String sort;
    private String location;
    private int resMusicId;
    private Music music;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "res_id")
    public int getResId() {
        return resId;
    }

    @Column(name = "res_sort")
    public String getSort() {
        return sort;
    }

    @Column(name = "res_location")
    public String getLocation() {
        return location;
    }


    @GenericGenerator(name = "gen", strategy = "property", parameters = @Parameter(name = "property",value = "id"))
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "gen")
    @Column(name = "res_music_id")
    public int getResMusicId() {
        return resMusicId;
    }


    public void setResId(int resId) {
        this.resId = resId;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public void setLocation(String location) {
        this.location = location;
    }


    public void setMusic(Music music) {
        this.music = music;
    }

    public void setResMusicId(int resMusicId) {
        this.resMusicId = resMusicId;
    }

    @Override
    public String toString() {
        return Arrays.toString(new String[]{"id:" + getResId(), "sort:" + getSort(), "location:" + getLocation(), "music-id:" + getResMusicId()});
    }


    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(referencedColumnName = "id", name = "res_music_id", insertable = false, updatable = false)
    public Music getMusic() {
        return music;
    }
}

音乐

package database;

import org.hibernate.annotations.*;

import javax.annotation.Nullable;
import javax.persistence.*;
import javax.persistence.AccessType;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Arrays;

/**
 * @author Jari Van Melckebeke
 */
@Entity
@Table(name = "music", catalog = "teresaDB")
public class Music implements Serializable {

    private int musicId;
    private String musicName;
    private String musicArtist;
    private String musicAlbum;
    private Resources resources;

    @Column(name = "track")
    public String getMusicName() {
        return musicName;
    }

    @Column(name = "artist")
    public String getMusicArtist() {
        return musicArtist;
    }

    @Id
    @Access(value = AccessType.PROPERTY)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getMusicId() {
        return musicId;
    }


    @Column(name = "album")
    @Nullable
    public String getMusicAlbum() {
        return musicAlbum;
    }


    public void setMusicId(int musicId) {
        this.musicId = musicId;
    }

    public void setMusicAlbum(String musicAlbum) {
        this.musicAlbum = musicAlbum;
    }

    public void setMusicArtist(String musicArtist) {
        this.musicArtist = musicArtist;
    }

    public void setMusicName(String musicName) {
        this.musicName = musicName;
    }

    @Override
    public String toString() {
        return Arrays.toString(new String[]{"id:" + getMusicId(), "track:" + getMusicName(), "artist:" + getMusicArtist(), "album:" + getMusicAlbum()});
    }

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(referencedColumnName = "res_music_id", name = "id", insertable = false, updatable = false)
    public Resources getResources() {
        return resources;
    }

    public void setResources(Resources resources) {
        this.resources = resources;
    }
}

mySQL

CREATE TABLE `resources` (
  `res_id` int(11) NOT NULL AUTO_INCREMENT,
  `res_sort` varchar(255) DEFAULT NULL,
  `res_location` varchar(255) DEFAULT NULL,
  `res_music_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`res_id`),
  KEY `mus_ind` (`res_music_id`),
  CONSTRAINT `resources_ibfk_1` FOREIGN KEY (`res_music_id`) REFERENCES `music` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1

CREATE TABLE `music` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `track` varchar(255) DEFAULT NULL,
  `artist` varchar(255) DEFAULT NULL,
  `album` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1

我知道,我不擅长'clean code'

提前致谢

在您的父实体(资源)中,您将连接(使用@JoinColumn)定义为 insertable = false,updatable = false,因此 creating/udpating 相关实体的责任不在当前实体。