您好我正在尝试插入记录,但获得以下例外情况:
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.
请帮我解决这个问题。在期待中感谢...
我的UserBean:
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");
}
我的用户实体:
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<Integer,String> status=new HashMap<Integer,String>();
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;
}
.. getters&amp;设置器....
我的Users.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 22 Aug, 2013 11:06:12 AM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="com.app.core.Users" table="users">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<one-to-one name="userinfo" class="com.app.core.Userinfo" cascade="all">
</one-to-one>
<many-to-one name="role" class="com.app.core.Role" fetch="select" lazy="false">
<column name="role" not-null="true" />
</many-to-one>
<many-to-one name="department" class="com.app.core.Department" fetch="select" lazy="false" not-found="ignore">
<column name="department" not-null="true" />
</many-to-one>
<property name="loginId" type="string">
<column name="loginid" length="64" not-null="true" unique="true" />
</property>
<property name="permission" type="int">
<column name="permission" length="1" not-null="true" default="1"/>
</property>
<property name="adminPower" type="int">
<column name="admin_power" length="1" not-null="true" default="0"/>
</property>
<!--<set name="processDetatilses" table="process_detatils" inverse="true" lazy="false" fetch="select">
<key>
<column name="fk_user" not-null="false" />
</key>
<one-to-many class="com.app.core.ProcessDetatils" />
</set>-->
<set name="processDetatilsesForParentUserfk" table="process_detatils" inverse="true" lazy="true" fetch="select">
<key>
<column name="parent_userfk" not-null="true" />
</key>
<one-to-many class="com.app.core.ProcessDetatils" />
</set>
<set name="processDetatilses" table="process_detatils" inverse="true" lazy="false" fetch="select">
<key>
<column name="fk_user" not-null="true" />
</key>
<one-to-many class="com.app.core.ProcessDetatils" />
</set>
</class>
</hibernate-mapping>
Plesse帮帮我......
答案 0 :(得分:2)
此处您已将Users实体类的id属性配置为自动生成的值
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
但是您要将属性ID的值设置为
int id = userDAO.getMaxUserId();
u.setId(id+1);
所以删除程序中的上面两行代码以避免约束违规异常。