无论如何使用提交的注释为项目字段设置ID并获取项目对象。所以我只能设置项目ID ,同时坚持这个子表而不是设置整个对象,因为我们要在board_project表中只保存项目的id。我是一个副本,我找不到其他链接。这就像我们正在使用提交注释而不是方法。
使用:hibernate5.0 - jpa2.1 lombok(即getter / setter)for Spring-data-commons-1.13 for CRUD Operations
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "board_project")
public class BoardProject {
@Id
@SequenceGenerator(name = "board_project_id_generator", sequenceName = "board_project_id_seq", allocationSize = 1)
@GeneratedValue(generator = "board_project_id_generator")
private Long id;
@Column(name = "board_id")
private Long boardId;
@ManyToOne
@JoinColumn(name = "project_id")
private Project project; // field set id and get object
}
项目实体:
public class Project{
@Id
@SequenceGenerator(name = "project_id_generator", sequenceName = "project_id_seq", allocationSize = 1)
@GeneratedValue(generator = "project_id_generator")
private Long id;
@Column(name = "name")
private String name;
}
更新:发现类似这样的内容
@ManyToOne @JoinColumn(name = "project_id", updatable = false, insertable = false) private Project project; @Column(name = "project_id") @NotNull private Long projectId;
不知道是否可以
对此的任何帮助都会很棒。