JPA将多点实体保存为外键持久

时间:2019-03-17 13:35:14

标签: spring-data-jpa

我有两个实体。董事会有许多BoardVersion。 BoardVersion仅具有一个董事会的外键。

@Entity
@Table(uniqueConstraints={@UniqueConstraint(columnNames={"name"})})
public class Board implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;

private String name;

@JsonIgnore
@OneToMany(mappedBy="board", fetch=FetchType.LAZY)
private Set<BoardVersion> boardVersions;

public Board(){

}

public Board(Long id, String name) {
    this.id = id;
    this.name = name;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Set<BoardVersion> getBoardVersions() {
    return boardVersions;
}

public void setBoardVersions(Set<BoardVersion> boardVersions) {
    this.boardVersions = boardVersions;
}

@Override
public String toString() {
    return "Board{" +
            "id=" + id +
            ", name='" + name + '\'' +
//                ", boardVersions=" + boardVersions +
            '}';
}
}

@Entity
public class BoardVersion implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;

@Column(name="board_id")
private Long boardId;

private String major;

private String minor;

private String revision;

@ManyToOne
@JoinColumn(name = "board_id", nullable=false, 
insertable=false,  updatable=false)
private Board board;

public BoardVersion(){

}

public BoardVersion(Long id, String major, String minor, 
String revision,Long boardId) {
    this.id = id;
    this.major = major;
    this.minor = minor;
    this.revision = revision;
    this.boardId = boardId;
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getMajor() {
    return major;
}

public void setMajor(String major) {
    this.major = major;
}

public String getMinor() {
    return minor;
}

public void setMinor(String minor) {
    this.minor = minor;
}

public String getRevision() {
    return revision;
}

public void setRevision(String revision) {
    this.revision = revision;
}

public Board getBoard() {
    return board;
}

public void setBoard(Board board) {
    this.board = board;
}

public Long getBoardId() {
    return boardId;
}

public void setBoardId(Long boardId) {
    this.boardId = boardId;
}
}

我通过SpringDataJPA将电路板和电路板版本保存到数据库中。

Board b1 = new Board(new Long(1) "A");
Board b2 = new Board(new Long(2),"B");
repositoryBoard.save(b2);
repositoryBoard.save(b3);

BoardVersion bv1 = new BoardVersion(new Long(1), "1", "2", "1", new Long(1));
BoardVersion bv2 = new BoardVersion(new Long(2), "1", "2", "3", new Long(1));
repositoryBoardVersion.save(bv1);
repositoryBoardVersion.save(bv2);            

表BoardVersion中只有一行。

板桌 身份证名 1个 2 B

BoardVersion id主要次要修订board_id 1 1 2 1 1

非常感谢您。

0 个答案:

没有答案