title: org.hibernate.exception.ConstraintViolationException: could not insert: [com.app.core.Users]
body: <p>Hi I am trying to insert record but getting the below exceptions: </p>
  
  <pre><code>INFO: uderId:::: 3
  INFO: Hibernate: insert into users (role, department, loginid, permission, admin_power) values (?, ?, ?, ?, ?)
  SEVERE: org.hibernate.exception.ConstraintViolationException: could not insert: [com.app.core.Users]
      at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
      at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
      at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:40)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2158)
      at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2638)
      at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:48)
      at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
      at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:298)
      at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
      at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:107)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
      at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
      at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
      at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
      at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
      at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
      at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
      at com.app.daoimpl.UserDAOImpl.save(UserDAOImpl.java:36)
      at com.app.core.UsersBean.execute(UsersBean.java:249)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:453)
      at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:292)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:255)
      at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:176)
      at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:265)
      at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
      at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:138)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
      at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:211)
      at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:190)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:90)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:243)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:141)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:145)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:171)
      at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:176)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:192)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:187)
      at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:249)
      at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
      at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:511)
      at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:432)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:217)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
      at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
      at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
      at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
      at com.sun.grizzly.http.ProcessorTask.
  SEVERE: doProcess(ProcessorTask.java:757)
      at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
      at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
      at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
      at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
      at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
      at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
      at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
      at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
      at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
      at java.lang.Thread.run(Thread.java:662)
  Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Cannot insert the value NULL into column 'id', table 'antrixedms.dbo.users'; column does not allow nulls. INSERT fails.
  </code></pre>
  
  <p>Please help me on this. Thanks in anticipation...</p>
  
  <p>My UserBean:</p>
  
  <pre><code>   UserDAO userDAO= UserDAOFactory.create();
              Users u=new Users();
              int id = userDAO.getMaxUserId();
              System.out.println("getMaxUserId:: "+id);
              RoleDAO roleDAO= RoleDAOFactory.create();
              Role r=roleDAO.getRole(this.role);
  
              u.setId(id+1);
              u.setRole(r);
  
              DepartmentDAO departmentDAO= DepartmentDAOFactory.create();
              Department d=departmentDAO.getDepartment(this.department);
              u.setDepartment(d);
  
              u.setLoginId(this.loginId);
              u.setPermission(Users.ACTIVE);
              u.setAdminPower(this.adminPower);
              Userinfo ui=new Userinfo();
              ui.setId(id+1);
              ui.setUserId(this.userId);
              ui.setFirstName(this.firstName);
              ui.setLastName(this.lastName);
              ui.setAddress(this.address);
              ui.setEmail(this.email);
              if(!this.mobile.equals("")){
                  this.mobile=this.countryCodeMob + "-" + this.mobile;
              }
              ui.setMobile(this.mobile);
              this.telephone=this.countryCodeTel + "-" + this.telephone;
              ui.setTelephone(this.telephone);
  
              if(!this.fax.equals("")){
                  this.fax=this.countryCodeFax + "-" + this.fax;
              }
              ui.setFax(this.fax);
  
              ui.setUserId(this.userId);
  
              CityDAO cityDAO= CityDAOFactory.create();
              ui.setCity(cityDAO.getCity(this.city));
              u.setUserinfo(ui);
              ui.setUser(u);
              try{
                  userDAO.save(u);
                  new ActivityLog(getUser(Integer.parseInt(this.usid)).getLoginId() , "User Creation :" +this.loginId, ActivityLog.SUCCESS,ActivityLog.ADMIN).writeLog();
                  jObj.addProperty("result", "SUCCESS");
              }catch(Exception e){
                  new ActivityLog(getUser(Integer.parseInt(this.usid)).getLoginId() , "User Creation :"+this.loginId, ActivityLog.FAILED,ActivityLog.ADMIN).writeLog();
                  jObj.addProperty("result", "FAILED");
                  e.printStackTrace();
              }
              //new SMTPUtil().sendMail("admin@takirasolutions.com", u.getLoginId(), "User Created", "Login using ur email Id and Password");
  
          }
  </code></pre>
  
  <p>My Users Entity:</p>
  
  <pre><code>public class Users  implements java.io.Serializable {
  
  
       private Integer id;
       private Userinfo userinfo;
       private Role role;
       private Department department;
       private String loginId;
       private int permission;
       private int adminPower;
       public static final int ADMIN=1;
       public static final int ACTIVE=1;
       public static final int DEACTIVE=0;
       public static final int TERMINATED=2;
       public static final int ADMIN_POWER=1;
       public static final String DOCUMENT_ROOT ="Documents";
  
       private Set processDetatilsesForParentUserfk = new HashSet(0);
       private Set processDetatilses = new HashSet(0);
  
       public static HashMap&lt;Integer,String&gt; status=new HashMap&lt;Integer,String&gt;();
       static{
          status.put(0,"Deactive");
          status.put(1,"Active");
          status.put(2,"Terminated");
       }   
  
      public Users() {
      }
  
      public Users(Userinfo userinfo, Role role, Department department, String loginId) {
         this.userinfo = userinfo;
         this.role = role;
         this.department = department;
         this.loginId = loginId;
      }
  </code></pre>
  
  <p>.. getters &amp; setters....</p>
  
  <p>My Users.hbm.xml:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  &lt;!-- Generated 22 Aug, 2013 11:06:12 AM by Hibernate Tools 3.2.1.GA --&gt;
  &lt;hibernate-mapping&gt;
      &lt;class name="com.app.core.Users" table="users"&gt;
          &lt;id name="id" type="java.lang.Integer"&gt;
              &lt;column name="id" /&gt;
              &lt;generator class="identity" /&gt;
          &lt;/id&gt;
          &lt;one-to-one name="userinfo" class="com.app.core.Userinfo" cascade="all"&gt;
          &lt;/one-to-one&gt;
          &lt;many-to-one name="role" class="com.app.core.Role" fetch="select" lazy="false"&gt;
              &lt;column name="role" not-null="true" /&gt;
          &lt;/many-to-one&gt;
          &lt;many-to-one name="department" class="com.app.core.Department" fetch="select" lazy="false" not-found="ignore"&gt;
              &lt;column name="department" not-null="true" /&gt;
          &lt;/many-to-one&gt;
          &lt;property name="loginId" type="string"&gt;
              &lt;column name="loginid" length="64" not-null="true" unique="true" /&gt;
          &lt;/property&gt;
          &lt;property name="permission" type="int"&gt;
              &lt;column name="permission" length="1" not-null="true" default="1"/&gt;
          &lt;/property&gt;
          &lt;property name="adminPower" type="int"&gt;
              &lt;column name="admin_power" length="1" not-null="true" default="0"/&gt;
          &lt;/property&gt;
  
         &lt;!--&lt;set name="processDetatilses" table="process_detatils" inverse="true" lazy="false" fetch="select"&gt;
              &lt;key&gt;
                  &lt;column name="fk_user" not-null="false" /&gt;
              &lt;/key&gt;
              &lt;one-to-many class="com.app.core.ProcessDetatils" /&gt;
          &lt;/set&gt;--&gt;
  
  
  
          &lt;set name="processDetatilsesForParentUserfk" table="process_detatils" inverse="true" lazy="true" fetch="select"&gt;
              &lt;key&gt;
                  &lt;column name="parent_userfk" not-null="true" /&gt;
              &lt;/key&gt;
              &lt;one-to-many class="com.app.core.ProcessDetatils" /&gt;
          &lt;/set&gt;
  
  
  
          &lt;set name="processDetatilses" table="process_detatils" inverse="true" lazy="false" fetch="select"&gt;
              &lt;key&gt;
              &lt;column name="fk_user" not-null="true" /&gt;
              &lt;/key&gt;
              &lt;one-to-many class="com.app.core.ProcessDetatils" /&gt;
          &lt;/set&gt;
  
   &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Plesse help me..</p>