I am new to hibernate
I got following classes
Code:
@Entity
@Table(name="applications")
public class Application implements java.io.Serializable {
private int id;
private Set<Job> jobs = new HashSet<Job>();
private String name;
private String curretnVersion;
private Date createdAt;
private Date updatedAt;
private String description;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="name",nullable=false, length=50, unique=true)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="current_version",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;
}
@OneToMany(fetch = FetchType.LAZY)
@NotNull
public Set<Job> getJobs() {
return jobs;
}
public void setJobs(Set<Job> jobs) {
this.jobs = jobs;
}
}
@Entity
@Table(name="job_statuses")
public class JobStatus {
int id;
String name;
String description;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
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;
}
}
@Entity
@Table(name = "job_types")
public class JobType {
private int id;
private String name;
private String description;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="name", nullable=false,length=20,unique=true)
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;
}
}
@Entity
@Table(name = "jobs")
public class Job implements java.io.Serializable {
private int Id;
private Job job;
private JobType jobType;
private JobStatus jobStatus;
private int priority;
private Set<Application> applications = new HashSet<Application>(0);
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
return Id;
}
public void setId(int id) {
Id = id;
}
@OneToOne(fetch = FetchType.LAZY)
@NotNull
@JoinColumn(name="job_type_id")
public JobType getJobType() {
return jobType;
}
public void setJobType(JobType jobType) {
this.jobType = jobType;
}
@OneToOne(fetch = FetchType.LAZY)
@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;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy="job")
@NotNull
public Set<Application> getApplications() {
return this.applications;
}
public void setApplications(Set<Application> applications) {
this.applications = applications;
}
}
and the main
Code:
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()));
Transaction tx = session.beginTransaction();
//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");
session.save(jType);
//Job Status
JobStatus js = new JobStatus();
js.setName("waiting");
session.save(js);
//first job
Job job1 = new Job();
job1.setPriority(3);
job1.setJobType(jType);
job1.setJobStatus(js);
job1.getApplications().add(app);
app.getJobs().add(job1);
session.saveOrUpdate(app);
session.saveOrUpdate(job1);
tx.commit();
session.close();
System.out.println((new SimpleDateFormat("HH:mm:ss:SSS")).format(new Date()));
}
It is OK when i run the main first time. When I ran the second time. I got the following error.
ERROR: duplicate key value violates unique constraint "uk_jidxac21t4vg1tmees14dwyo1"
Detail: Key (name)=(banboo) already exists.
How to solve the problem?
Thanks