我有一个Job类,如下所示
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
@ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.PERSIST)
@NotNull
@JoinColumn(name="job_type_id")
public JobType getJobType() {
return jobType;
}
public void setJobType(JobType jobType) {
this.jobType = jobType;
}
@ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.PERSIST)
@NotNull
@JoinColumn(name="job_status_id")
public JobStatus getJobStatus() {
return jobStatus;
}
public void setJobStatus(JobStatus jobStatus) {
this.jobStatus = jobStatus;
}
@Column(name="priority",nullable=false)
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
@ManyToOne(fetch = FetchType.LAZY, cascade=CascadeType.PERSIST)
@NotNull
@JoinColumn(name="applliaction_id")
public Application getApplication() {
return application;
}
public void setApplication(Application application) {
this.application = application;
}
}
JobStatus类就像
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="name",nullable =false, unique=true, length=50)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="description",columnDefinition = "text")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
和JobType类一样
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="name", unique=true, nullable=false,length=20)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="description",columnDefinition = "text")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
和应用程序类如
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="name",nullable=false,unique =true, length=50)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="current_version",nullable=false,length=50)
public String getCurretnVersion() {
return curretnVersion;
}
public void setCurretnVersion(String curretnVersion) {
this.curretnVersion = curretnVersion;
}
@Column(name="updated_at",nullable=false)
public Date getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Date updatedAt) {
this.updatedAt = updatedAt;
}
@Column(name="created_at",nullable=false)
public Date getCreatedAt() {
return createdAt;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
@Column(name="description",columnDefinition = "text")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
主要方法就像
public static void main(String[] args) {
System.out.println((new SimpleDateFormat("HH:mm:ss:SSS")).format(new Date()));
Session session = HibernateUtil.getSessionFactory().openSession();
System.out.println((new SimpleDateFormat("HH:mm:ss:SSS")).format(new Date()));
//Application
Application app = new Application();
app.setName("GW");
app.setCreatedAt(new Date());
app.setUpdatedAt(new Date());
app.setCurretnVersion("1.0");
//Job Type
JobType jType = new JobType();
jType.setDescription("new type");
jType.setName("banboo");
//Job Status
JobStatus js = new JobStatus();
js.setName("waiting");
//first job
Job job1 = new Job();
job1.setPriority(3);
job1.setApplication(app);
job1.setJobType(jType);
job1.setJobStatus(js);
//Second Job
Job job2 = new Job();
job2.setPriority(3);
job2.setApplication(app);
job2.setJobStatus(js);
job2.setJobType(jType);
Transaction tx = session.beginTransaction();
session.save(job1);
session.save(job2);
session.save(jType);
session.save(js);
session.save(app);
tx.commit();
session.close();
System.out.println((new SimpleDateFormat("HH:mm:ss:SSS")).format(new Date()));
}
我第一次跑主要是OK。但是当我重新运行时。得到以下错误。 重复键值违反唯一约束“uk_jidxac21t4vg1tmees14dwyo1” 细节:键(名称)=(banboo)已经存在。
如何解决问题?
谢谢,
斯科特
按照JB Nizet的建议编辑程序和问题。
答案 0 :(得分:0)
你告诉Hibernate该关联是OneToOne
。
这意味着只有一个Job可以引用给定的JobStatus,并且给定的Job只能引用一个JobStatus。
如果您想要多个职位共享一个JobStatus,则需要ManyToOne
。