FundsAccount持久化类:
private Integer fundsAccountId;
......
private Set upFundsAccountLogses = new HashSet(0);
UpFundsAccountLogses 持久化类:
private Integer id;
......
private FundsAccount fundsAccount;addAccount方法中:我在向数据库插入一个新的记录时(同时也把fundsAccount的一个列(prise列)做了改变):
String statu=null;
String fundType=FundAccountAdminDao.queryType(numberedAccount);
if(fundType.equals("正常"))
{
Session session=HibernateSessionFactory.getSession();
Transaction tr=null;
try {
Query q=session.createQuery("from FundsAccount where numberedAccount='"+numberedAccount+"'");
FundsAccount fundsAccount=(FundsAccount)q.uniqueResult();
double fundPrise=fundsAccount.getAmountOfMoney();
fundPrise=fundPrise+prise;
fundsAccount.setAmountOfMoney(fundPrise);
UpFundsAccountLogs upFundsAccountLogs=new UpFundsAccountLogs();
System.out.println(upFundsAccountLogs.getId());
upFundsAccountLogs.setOperators(operator);
upFundsAccountLogs.setFundsAccount(fundsAccount);
upFundsAccountLogs.setUpdateTime(new Date());
tr=session.beginTransaction();
fundsAccount.getUpFundsAccountLogses().add(upFundsAccountLogs);
session.save(fundsAccount);
//session.save(upFundsAccountLogs);
tr.commit();
statu="追加资金成功!";
表FundsAccount中的AmountOfMoney字段修改成功,但UpFundsAccountLogses表没有插入新记录.又写了一个方法,但测向表UpFundsAccountLogses中插入数据:
public void upFundsLogs(String numberedAccount,Operators operator){
Session session=HibernateSessionFactory.getSession();
Query q=session.createQuery("from FundsAccount where numberedAccount='"+numberedAccount+"'");
FundsAccount fundsAccount=(FundsAccount)q.uniqueResult();
UpFundsAccountLogs upFundsAccountLogs=new UpFundsAccountLogs();
System.out.println(upFundsAccountLogs.getId());
upFundsAccountLogs.setOperators(operator);
upFundsAccountLogs.setFundsAccount(fundsAccount);
upFundsAccountLogs.setUpdateTime(new Date());
session.persist(upFundsAccountLogs);
}
测试出错:
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.id.IncrementGenerator.getNext(IncrementGenerator.java:107)
at org.hibernate.id.IncrementGenerator.generate(IncrementGenerator.java:44)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:91)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:131)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:87)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:38)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:613)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:587)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:591)
at com.dao.fundAccountAdmin.FundAccountAdminDao.upFundsLogs(FundAccountAdminDao.java:317)
at com.dao.fundAccountAdmin.FundAccountAdminDao.main(FundAccountAdminDao.java:334)
Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]对象名 'upFundsAccountLogs' 无效。
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSExecuteRequest.processReplyToken(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)
at org.hibernate.id.IncrementGenerator.getNext(IncrementGenerator.java:85)
... 10 more

解决方案 »

  1.   

    错误是upFundsAccountLogses获取取主键失败,你的upFundsAccountLogses的XML文件和对应的表是怎样的
      

  2.   

    我是想在追加资金同时记录操作员Operators的操作记录
      

  3.   

    upFundsAccountLogses的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">
    <!-- 
        Mapping file autogenerated by MyEclipse - Hibernate Tools
    -->
    <hibernate-mapping>
        <class name="com.hibernate.pojo.UpFundsAccountLogs" table="upFundsAccountLogs" schema="dbo" catalog="BankFund">
            <id name="id" type="java.lang.Integer">
                <column name="id" />
                <generator class="increment" />
            </id>
            <many-to-one name="operators" class="com.hibernate.pojo.Operators" fetch="select">
                <column name="operatorId" not-null="true" />
            </many-to-one>
            <many-to-one name="fundsAccount" class="com.hibernate.pojo.FundsAccount" fetch="select">
                <column name="fundsAccountId" not-null="true" />
            </many-to-one>
            <property name="updateTime" type="java.util.Date">
                <column name="updateTime" length="23" not-null="true" />
            </property>
            <property name="describes" type="java.lang.String">
                <column name="describes" length="200" />
            </property>
        </class>
    </hibernate-mapping>
    对应FundsAccoun的XML文件里为:
            <set name="upFundsAccountLogses" inverse="true">
                <key>
                    <column name="fundsAccountId" not-null="true" />
                </key>
                <one-to-many class="com.hibernate.pojo.UpFundsAccountLogs" />
            </set>另,我在上面的addAccount方法里
    tr.commit();
    statu="追加资金成功!";
    中间加了这个测试,也是得到id为空,但能得到别的值.:
    Set uf=fundsAccount.getUpFundsAccountLogses();
    Iterator it=uf.iterator();
    while(it.hasNext()){
    UpFundsAccountLogs upF=(UpFundsAccountLogs) it.next();
    System.out.println(upF.getId());
    System.out.println(upF.getUpdateTime());
    }
      

  4.   

    我把对应FundsAccoun的XML文件改为:
    <set name="upFundsAccountLogses" table="upFundsAccountLogs" inverse="true" cascade="delete" lazy="true">
                <key>
                    <column name="fundsAccountId" not-null="true" />
                </key>
                <one-to-many class="com.hibernate.pojo.UpFundsAccountLogs" />
            </set>
    还是不行.
    出:
    null
    null
    Thu Nov 30 17:41:59 CST 2006
    追加资金成功!
      

  5.   

    数据库没问题,id为主键,自动增长
    =====================================
    假如你的数据库主键是可以自动增长的就不能用
    <generator class="increment" />
    而用
    <generator class="identity" />  
    了,不知道你用什么数据库,主键怎么定义的?!
      

  6.   

    <many-to-one not-null="false"/>
    应该是这个
      

  7.   

    夏老大书上有个例子,user和address一对多的关系,user和address采用双向映射关系,在user中一对多inverse=true;
    有问题代码:
    transcation tx = session.beginTranscation();
    Address addr = new Address();
    addr.setTel("1112");
    addr.setZipCode("4545");user.getAddress().add(addr);
    session.save(user);//通过主控对象更新
    tx.commit();更正代码:transcation tx = session.beginTranscation();
    Address addr = new Address();
    addr.setTel("1112");
    addr.setZipCode("4545");user.getAddress().add(addr);addr.setUser(user);//设置关联的User对象十分重要,上个代码NUll操作了session.save(user);//通过主控对象更新
    tx.commit();
      

  8.   

    哦,sorry加错位置了,
    user.getAddress().add(addr);addr.setUser(user);//设置关联的User对象十分重要,上个代码NUll操作了
    两行换一下!