how to solve custom primary key duplication in one to many?
In my code ,I set custom primary key in one to many.
But when I save the object error "a different object with the same identifier value was already associated with the session:"
custom primary key ,tableB get primary key error
if object A have two B,The two table“B”get the same primary key,so when I save the object A ,it failed.
how to solve the problem?
table A
Code:
<hibernate-mapping>
<class name="com.intron.orm.CalendarDetail" table="CalendarDetail"
dynamic-insert="true" dynamic-update="true">
<id name="IDetail" type="string">
<column name="i_detail" length="12" />
<generator class="com.intron.tools.web.IncrementGeneratorString" />
</id>
<property name="ICompany" type="string">
<column name="i_company" length="12" />
</property>
<property name="IEmployee" type="string">
<column name="i_employee" length="12" />
</property>
<property name="IType" type="java.lang.Integer">
<column name="i_type" />
</property>
<property name="IImportant" type="java.lang.Integer">
<column name="i_important" />
</property>
<property name="IKind" type="string">
<column name="i_kind" length="12" />
</property>
<property name="IName" type="string">
<column name="i_name" length="256" />
</property>
<property name="IDealNo" type="string">
<column name="i_dealNo" length="24" />
</property>
<property name="IDealMemo" type="string">
<column name="i_dealMemo" length="256" />
</property>
<property name="IActDate" type="timestamp">
<column name="i_actDate" length="23" />
</property>
<property name="IClerk" type="string">
<column name="i_clerk" length="12" />
</property>
<property name="IFinish" type="java.lang.Character">
<column name="i_finish" length="1" />
</property>
<property name="ICircleType" type="java.lang.Integer">
<column name="i_circleType" />
</property>
<property name="IDueDate" type="timestamp">
<column name="i_dueDate" length="23" />
</property>
<property name="IDueDateTo" type="timestamp">
<column name="i_dueDateTo" length="23" />
</property>
<property name="IPreDay" type="java.lang.Integer">
<column name="i_preDay" />
</property>
<property name="IAfterDay" type="java.lang.Integer">
<column name="i_afterDay" />
</property>
<property name="ISeperate" type="java.lang.Integer">
<column name="i_seperate" />
</property>
<property name="IActType" type="java.lang.Integer">
<column name="i_actType" />
</property>
<property name="IMonth" type="java.lang.Integer">
<column name="i_month" />
</property>
<property name="IDay" type="java.lang.Integer">
<column name="i_day" />
</property>
<property name="IWeek" type="java.lang.Integer">
<column name="i_week" />
</property>
<property name="IBegDate" type="timestamp">
<column name="i_begDate" length="23" />
</property>
<property name="IEndDate" type="timestamp">
<column name="i_endDate" length="23" />
</property>
<property name="IResult" type="string">
<column name="i_result" length="120" />
</property>
<property name="IParant" type="string">
<column name="i_parant" length="12" />
</property>
<property name="IState" type="java.lang.Integer">
<column name="i_state" />
</property>
<property name="ICustomer" type="string">
<column name="i_customer" length="12" />
</property>
<property name="IResPact" type="string">
<column name="i_resPact" length="12" />
</property>
<property name="IResVary" type="string">
<column name="i_resVary" length="12" />
</property>
<set name="funMan" table="CalendarFunMan" lazy="false"
cascade="save-update,delete-orphan">
<key column="i_from" />
<one-to-many class="com.intron.orm.CalendarFunMan" />
</set>
</class>
</hibernate-mapping>
table B
Code:
<hibernate-mapping>
<class name="com.intron.orm.CalendarFunMan" table="CalendarFunMan"
dynamic-insert="true" dynamic-update="true" >
<id name="IFunMan" type="string">
<column name="i_funMan" length="12" />
<generator class="com.intron.tools.web.IncrementGeneratorString" />
</id>
<property name="ICompany" type="string">
<column name="i_company" length="12" not-null="true" />
</property>
<property name="IEmployee" type="string">
<column name="i_employee" length="12" />
</property>
<property name="IFrom" type="string">
<column name="i_from" length="12" />
</property>
<property name="IType" type="java.lang.Integer">
<column name="i_type" />
</property>
</class>
</hibernate-mapping>
com.intron.tools.web.IncrementGeneratorString:
Code:
package com.intron.tools.web;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.type.Type;
public class IncrementGeneratorString implements IdentifierGenerator, Configurable {
private static final Log log = LogFactory.getLog(IncrementGeneratorString.class);
private Long next;
private String nexts;
private String sql;
public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
if (sql!=null) {
getNexts( session.connection() );
}
return nexts;
}
public void configure(Type type, Properties params, Dialect d) throws MappingException {
String table = params.getProperty("table");
if (table==null) table = params.getProperty(PersistentIdentifierGenerator.TABLE);
String column = params.getProperty("column");
if (column==null) column = params.getProperty(PersistentIdentifierGenerator.PK);
String schema = params.getProperty(PersistentIdentifierGenerator.SCHEMA);
sql = "select max("+column +") from " + ( schema==null ? table : schema + '.' + table );
log.info(sql);
}
private void getNexts(Connection conn) throws HibernateException {
try {
PreparedStatement st = conn.prepareStatement(sql);
ResultSet rs = st.executeQuery();
if ( rs.next() ) {
nexts = rs.getString(1);
if(nexts==null){
nexts = "A00000000001";
}else{
String as =nexts.substring(0,2);
String sa = nexts.substring(2,nexts.length());
Integer.valueOf(sa);
sa=String.valueOf(Integer.valueOf(sa)+1);
for(int i=0;i<(12-2-sa.length());i++){
as=as+"0";
}
nexts=as+sa;
}
}
else {
nexts = "A00000000001";
}
}catch(SQLException e)
{
throw new HibernateException(e);
}
finally {
try{
conn.close();
}catch(SQLException e)
{
throw new HibernateException(e);
}
}
}
private void getNextsbk(Connection conn) throws HibernateException {
try {
PreparedStatement st = conn.prepareStatement(sql);
ResultSet rs = st.executeQuery();
if ( rs.next() ) {
nexts = rs.getString(1);
String as =nexts.substring(0,2);
String sa = nexts.substring(2,nexts.length());
Integer.valueOf(sa);
sa=String.valueOf(Integer.valueOf(sa)+1);
for(int i=0;i<(12-2-sa.length());i++){
as=as+"0";
}
nexts=as+sa;
}
else {
nexts = "A00000000001";
}
}catch(SQLException e)
{
throw new HibernateException(e);
}
finally {
try{
conn.close();
}catch(SQLException e)
{
throw new HibernateException(e);
}
}
}
private void getNext(Connection conn) throws HibernateException {
try {
PreparedStatement st = conn.prepareStatement(sql);
ResultSet rs = st.executeQuery();
if ( rs.next() ) {
next = rs.getLong(1) + 1;
}
else {
next = 1l;
}
}catch(SQLException e)
{
throw new HibernateException(e);
}
finally {
try{
conn.close();
}catch(SQLException e)
{
throw new HibernateException(e);
}
}
}
public static void main(String[] args){
String a="Abc00000000100";
a.substring(1, a.length());
//int aa= Integer.valueOf(a.substring(1, a.length()));
System.out.println("aa::"+a.substring(1, a.length()));
System.out.println("aa::"+a.substring(0,2));
//aa++;
//System.out.println("aa+1::"+aa);
}
}