HIbernate唯一的列值

时间:2015-03-05 23:08:19

标签: hibernate unique one-to-many one-to-one many-to-one

我是Hibernate的新手。 我有以下课程

@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;
    }

    @ManyToMany(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;
    }


    @ManyToMany(fetch = FetchType.LAZY)
    @NotNull
    public Set<Application> getApplications() {
        return this.applications;
    }
    public void setApplications(Set<Application> applications) {
        this.applications = applications;
    }
}

主要

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.save(app);
    session.save(job1);
    tx.commit();
    session.close();
    System.out.println((new SimpleDateFormat("HH:mm:ss:SSS")).format(new Date()));
}

设计是 作业只有一个作业状态,只有一个作业类型,多个应用程序。 工作状态可以属于多个工作。 作业类型可以属于多个作业。 一个应用程序可以属于多个作业。

我有两个问题

1 :(这是通过JB Nizet的帮助解决的) 我收到了一个错误

列“job_id”中的

空值违反了非空约束   细节:失败的行包含(90,3,89,3,null)。

如何解决这个问题?

2:

当id首次运行main时。一切都很好。但第二次运行它得到了下面的错误

错误:重复键值违反唯一约束“uk_jidxac21t4vg1tmees14dwyo1”   细节:键(名称)=(banboo)已经存在。

在设计中,可以多次输入相同的作业。

由于

0 个答案:

没有答案