-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: unique column value question
PostPosted: Thu Mar 05, 2015 7:42 pm 
Newbie

Joined: Thu Mar 05, 2015 7:37 pm
Posts: 1
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


Top
 Profile  
 
 Post subject: Re: unique column value question
PostPosted: Fri Mar 06, 2015 9:07 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi,

the problem is that you have defined the attribute name in JobType as unique:

Code:
    @Column(name="name", nullable=false,length=20,unique=true)
    public String getName() {
        return name;
    }


In the main, you create a new JobType every time but you don't change the name, this result in two entries with the same name causing the unique constraint exception.

Hope this help,
Davide


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.