首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

ssh框架有关问题困扰小弟我好几天了

2012-09-23 
ssh框架问题困扰我好几天了啊我插入数据两张表一对一的主键关联,服务器启动以后第一次可以插入数据,第二次

ssh框架问题困扰我好几天了啊
我插入数据两张表一对一的主键关联,服务器启动以后第一次可以插入数据,第二次就提示主键重复,在重启服务器以后,第一条可以插入,第二条又提示主键重复了啊!郁闷啊!!!
这是save方法,LoginId是用户名不是seq
@Transactional(propagation=Propagation.REQUIRED)
public String add(){
logger.info("add user access");
UserDAO userDao = (UserDAO)applicationContext.getBean("userDAO");
user.setLoginId(user.getLoginId());
user.setPassword(genRandomPassword(8));
user.setStatus((short)1);
user.setCategory(user.getCategory());
Date date = new Date();
user.setLastLogin(date);
individual.setCreateBy(1);
individual.setFristName(individual.getFristName());
individual.setLastName(individual.getLastName());
individual.setGreeting(mr);
individual.setCreateTime(new Date());
individual.setEmail(individual.getEmail());
individual.setEmailVerified(false);
user.setIndividual(individual);
userDao.save(user);
return "success";
}
错误异常:
Struts Problem Report

Struts has detected an unhandled exception:
Messages:

  Duplicate entry '122' for key 1
  Could not execute JDBC batch update
  Could not execute JDBC batch update; SQL [insert into aicloud_poc.User (LoginID, Password, LastLogin, Status, Category, ProfileID, Seq) values (?, ?, ?, ?, ?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

File: com/mysql/jdbc/ServerPreparedStatement.java
Line number: 665
Stacktraces
org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; SQL [insert into aicloud_poc.User (LoginID, Password, LastLogin, Status, Category, ProfileID, Seq) values (?, ?, ?, ?, ?, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

  org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:643)



xml配置文件 user
<?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 Jun 29, 2012 12:01:12 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
  <class name="cn.com.aicontrol.aicloud.entity.User" table="User" catalog="aicloud_poc" dynamic-update="true">
  <meta attribute="generated-class" inherit="false">cn.com.aicontrol.aicloud.entity.AbstractUser</meta>
  <meta attribute="scope-class" inherit="false">public abstract</meta>
  <id name="seq" type="java.lang.Integer">
  <column name="Seq" />
  <generator class="foreign">
  <param name="property">individual</param>
  </generator>
  </id>
  <one-to-one name="individual" class="cn.com.aicontrol.aicloud.entity.Individual" constrained="true" cascade="all" lazy="false"></one-to-one>
  <property name="loginId" type="string">
  <column name="LoginID" length="30" not-null="true" unique="true" />
  </property>
  <property name="password" type="string">
  <column name="Password" length="128" not-null="true" />
  </property>
  <property name="lastLogin" type="timestamp">


  <column name="LastLogin" length="19" not-null="true" />
  </property>
  <property name="status" type="short">
  <column name="Status" not-null="true" />
  </property>
  <property name="category" type="char">
  <column name="Category" length="1" not-null="true">
  <comment>A:</comment>
  </column>
  </property>
  <property name="profileId" type="java.lang.Integer">
  <column name="ProfileID" />
  </property>
  <set name="userFunctions" table="UserFunction" inverse="true" lazy="true" fetch="select">
  <key>
  <column name="UserID" not-null="true" />
  </key>
  <one-to-many class="cn.com.aicontrol.aicloud.entity.UserFunction" />
  </set>
  <set name="userGroups" table="UserGroup" inverse="true" lazy="true" fetch="select">
  <key>
  <column name="UserSeq" not-null="true" />
  </key>
  <one-to-many class="cn.com.aicontrol.aicloud.entity.UserGroup" />
  </set>
  </class>
</hibernate-mapping>
这是个人信息表的配置文件
<?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 Jun 29, 2012 12:01:12 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
  <class name="cn.com.aicontrol.aicloud.entity.Individual" table="Individual" catalog="aicloud_poc"
  dynamic-update="true">
  <meta attribute="generated-class" inherit="false">cn.com.aicontrol.aicloud.entity.AbstractIndividual</meta>
  <meta attribute="scope-class" inherit="false">public abstract</meta>
  <id name="seq" type="java.lang.Integer">
  <column name="Seq" />
  <generator class="native" />
  </id>
  <property name="fristName" type="string">
  <column name="FristName" length="45" />
  </property>
  <property name="lastName" type="string">
  <column name="LastName" length="45" />
  </property>
  <property name="greeting" type="string">
  <column name="Greeting" length="45" />
  </property>
  <property name="email" type="string">
  <column name="Email" length="45" />
  </property>
  <property name="emailVerified" type="boolean">
  <column name="EmailVerified" not-null="true" />
  </property>
  <property name="createTime" type="timestamp">


  <column name="CreateTime" length="19" />
  </property>
  <property name="createBy" type="java.lang.Integer">
  <column name="CreateBy" />
  </property>
  <set name="customersForPriContact" table="Customer" inverse="true" lazy="true" fetch="select">
  <key>
  <column name="PriContact" />
  </key>
  <one-to-many class="cn.com.aicontrol.aicloud.entity.Customer" />
  </set>
  <one-to-one name="user" class="cn.com.aicontrol.aicloud.entity.User"></one-to-one>
  <set name="customersForBakContact" table="Customer" inverse="true" lazy="true" fetch="select">
  <key>
  <column name="BakContact" />
  </key>
  <one-to-many class="cn.com.aicontrol.aicloud.entity.Customer" />
  </set>
  </class>
</hibernate-mapping>



[解决办法]
user.setLoginId(user.getLoginId());
如果是自增的哪你就不用去拿ID啊
[解决办法]
<id name="seq" type="java.lang.Integer">
<column name="Seq" />
<generator class="foreign">
<param name="property">individual</param>
</generator>
</id>

<id name="seq" type="java.lang.Integer">
<column name="Seq" />
<generator class="native" />
</id>

2个问题
1、主从表搞反了,user应该是主表吧,就是的设置应该反过来;
2、不应该是<generator class="native" />,应该是<generator class="increment" />
[解决办法]
user表的主键不要设自增。

热点排行