@Entity
@Table(name = "artist")
public class Artist implements java.io.Serializable{
@Basic
@Column(name = "is_active",nullable=false)
private Boolean isActive = false;
@OneToMany(mappedBy = "artist", cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
private Set<Project> projects= new HashSet<Project>();
}
和
@Entity
@Table(name = "project")
public class Project implements java.io.Serializable{
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "artist_id")
private Artist artist;
@Basic
@Column(name = "is_active",nullable=false)
private Boolean isActive = false;
}
我可以在isActive
中初始化Project
,如下所示,意图Project
的旗帜由Artist
的旗帜初始化
private Boolean isActive = ((getArtist()!=null)? getArtist().getIsActive(): false);
在Project
中定义成员的顺序是否会导致isActive
始终为假?
答案 0 :(得分:1)
声明:
private Boolean isActive = ((getArtist()!=null)? getArtist().getIsActive(): false);
isActive将始终为false,因为artist和isActive将在Project的默认构造函数之前初始化。因此,将artist初始化为null,然后将isActive初始化为false(因为getArtist()为null),然后Project的默认构造函数运行(但构造函数不执行任何操作),因此isActive始终为false。