1

im new to Spring and hibernate, i got the error above when trying to persist the transaction data. please try to help this problem Here's my Entity:

 @Entity @NamedQuery(name="Employee.findAll", query="SELECT e FROM    Employee e") 

    public class Employee implements Serializable{      

    private    static final long serialVersionUID = 1L;     
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)   
    private int id;
    private String city;    
    private String civil;
    @Temporal(TemporalType.DATE)    
    @Column(name="dob", length=11)
    private Date dob;   
    private String email;   
    private int epf;    
    private String fname;   
    private String gender;  
    private int landtp;     
    private String lname;   
    @Temporal(TemporalType.DATE)
    @Column(name="salaryincrement", length=11)  
   //bi-directional many-to-one association to    Designation   
   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name="designation_id", nullable=false)   
   private    Designation designation;  

   public Employee() {  }



    @Entity
    @NamedQuery(name="Designation.findAll", query="SELECT d FROM Designation d")
    public class Designation implements Serializable{ 
      private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    private String type;

    //bi-directional many-to-one association to Employee
    @OneToMany(mappedBy="designation")//, cascade=CascadeType.ALL
    private List<Employee> employees;

    public Designation() {
            }

this is my Entity class, Entities have a getters ans setters

Tharaka
  • 189
  • 2
  • 4
  • 20

2 Answers2

0

designation is set nullable = false. However employees variable isn't initialized in Designation. So, you'll have to initialize as

@OneToMany(mappedBy="designation")//, cascade=CascadeType.ALL
private List<Employee> employees = new LinkedList<>();
TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
  • dear sir, Still have a same error in my code, is right my mapping annotation? – Tharaka Sep 18 '13 at 13:29
  • Try removing `nullable=false` in `@JoinColumn(name="designation_id", nullable=false)`. Afterall, it can also be handled in application level rather than in database level. – TheKojuEffect Sep 18 '13 at 13:31
  • yes, it works but another error become "SEVERE: Servlet.service() for servlet [spring] in context with path [/CEB] threw exception [Request processing failed; nested exception is org.hibernate.exception.ConstraintViolationException: could not insert: [com.tharaka.model.Employee]] with root cause com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'designation_id' cannot be null" – Tharaka Sep 18 '13 at 13:36
  • You may ask another question with more details for that. Also your entity definition is quite messy as you've `@Temporal(TemporalType.DATE)` for `designation` – TheKojuEffect Sep 18 '13 at 13:40
  • Are you sure your `designation_id` column is `AUTO_INCREMENT` in database schema? – Boris Treukhov Sep 18 '13 at 13:43
  • Select query works, it generate Employee details in view, but inset dose not work – Tharaka Sep 18 '13 at 13:54
  • Post the stacktrace while inserting. Post to [http://pastebin.com/](http://pastebin.com/) and share the link here. – TheKojuEffect Sep 18 '13 at 14:13
  • is error in here, because i didn't pass a designation object to constructor method........ public Employee(Integer id, int epf, String fname, String lname, String line1, String line2, String city, String nic, String email, int mobiletp, int landtp, Date dob, Date salaryincrement,String civil, String gender, Designation designation); – Tharaka Sep 18 '13 at 16:44
  • It would be better if you post another question regarding this issue. May be other people can help. – TheKojuEffect Sep 18 '13 at 16:48
0

I'm not sure that you can go with primitive type int as your Id - you should probably use Integer - because int has default zero value and cannot be null, your new record can be rather seen as a detached entity with Id ZERO and not as a transient one.

The same mistake is in Designation class.

See Primitive or wrapper for hibernate primary keys

Community
  • 1
  • 1
Boris Treukhov
  • 17,493
  • 9
  • 70
  • 91
  • Dear sir I have tried but still have a same error in my code, please give me another way.. – Tharaka Sep 18 '13 at 13:25
  • Change the id part and please post the actual code that is saving entities. The relationship between Employee and Designation is not cascade - are you referencing a not saved `Designation` from `Employee`? – Boris Treukhov Sep 18 '13 at 13:29
  • If the relationship is cascade you should probably use `cascade={CascadeType.PERSIST, ...}` otherwise you should not. http://www.mkyong.com/hibernate/hibernate-cascade-example-save-update-delete-and-delete-orphan/ – Boris Treukhov Sep 18 '13 at 13:32
  • From the design point of view `Designation` should not be cascaded because several employees can have same *designation* so you should save it with separate save/persist call on the session, it does not seem very good that Designation will be created with Employee entity. – Boris Treukhov Sep 18 '13 at 13:39
  • Select query works, it generate Employee details in view, but inset dose not work – Tharaka Sep 18 '13 at 13:55
  • How are Employee and Designation entities persisted? If something doesn't work, show it to the SO community. Add the code with the relevant save/persist calls. – Boris Treukhov Sep 18 '13 at 13:57
  • Where do you assign designation property of Employee? – Boris Treukhov Sep 18 '13 at 14:02