google-code-export / nutz

Automatically exported from code.google.com/p/nutz
Apache License 2.0
1 stars 1 forks source link

1.38PoJo中有@Readonly字段时,dao.insert会出错。 #491

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
1.38PoJo中有@Readonly字段时,dao.insert会出错。

Original issue reported on code.google.com by wangying...@gmail.com on 31 May 2011 at 7:50

GoogleCodeExporter commented 9 years ago
Pojo:
@PK( { "generalID", "categoryID" })
public class ArticleCategorys {
          @Column
    private Integer generalID;

    @Column
    private Integer categoryID;

       @Readonly   
    @Column
    private Integer parentID;

}
nutz:nutz-1.b.38-snapshot-20110529_1246AM-jdk6.jar

Original comment by wangying...@gmail.com on 31 May 2011 at 8:18

GoogleCodeExporter commented 9 years ago
r2107 普通POJO无问题

Original comment by wendal1985@gmail.com on 31 May 2011 at 1:26

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
完整的pojo:
package com..frame.pojo.factory;

import org.nutz.dao.entity.annotation.*;

@PK( { "generalID", "categoryID" })
@View("(select a.GeneralID,c.CategoryID,c.CategoryName,c.ParentID from 
Factory.dbo.ArticleCategorys as a left join Factory.dbo.Category as c on 
a.CategoryID=c.CategoryID) as ArticleCategorys")
@Table("Factory.dbo.ArticleCategorys")
public class ArticleCategorys {

    @Column
    private Integer generalID;

    @Column
    private Integer categoryID;

    @Readonly
    @Column
    private String categoryName;

    @Readonly
    @Column
    private Integer parentID;

    public Integer getGeneralID() {
        return generalID;
    }

    public void setGeneralID(Integer generalID) {
        this.generalID = generalID;
    }

    public Integer getCategoryID() {
        return categoryID;
    }

    public void setCategoryID(Integer categoryID) {
        this.categoryID = categoryID;
    }

    public String getCategoryName() {
        return categoryName;
    }

    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }

    public Integer getParentID() {
        return parentID;
    }

    public void setParentID(Integer parentID) {
        this.parentID = parentID;
    }

}
完整的异常:
org.nutz.filepool.NutFilePool - Init file-pool by: ~/.nutz/tmp/dao/ [2000]
org.nutz.filepool.NutFilePool - file-pool.cursor: 0
org.nutz.dao.impl.sql.run.NutDaoExecutor - INSERT INTO 
Factory.dbo.ArticleCategorys(generalID) VALUES(?) 
    |        1 |
    |----------|
    | 20002122 |
  For example:> "INSERT INTO Factory.dbo.ArticleCategorys(generalID) VALUES(20002122) "
Exception in thread "main" org.nutz.dao.DaoException: 
org.nutz.dao.DaoException: !Nutz SQL Error: 'INSERT INTO 
Factory.dbo.ArticleCategorys(generalID) VALUES(?) 
    |        1 |
    |----------|
    | 20002122 |
  For example:> "INSERT INTO Factory.dbo.ArticleCategorys(generalID) VALUES(20002122) "'
PreparedStatement: 
'INSERT INTO Factory.dbo.ArticleCategorys(generalID) VALUES(?) '
    at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:54)
    at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:178)
    at org.nutz.dao.impl.EntityOperator.exec(EntityOperator.java:49)
    at org.nutz.dao.impl.NutDao.fastInsert(NutDao.java:121)
    at com..test.TestArticleCategorys.main(TestArticleCategorys.java:21)
Caused by: org.nutz.dao.DaoException: !Nutz SQL Error: 'INSERT INTO 
Factory.dbo.ArticleCategorys(generalID) VALUES(?) 
    |        1 |
    |----------|
    | 20002122 |
  For example:> "INSERT INTO Factory.dbo.ArticleCategorys(generalID) VALUES(20002122) "'
PreparedStatement: 
'INSERT INTO Factory.dbo.ArticleCategorys(generalID) VALUES(?) '
    at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:149)
    at org.nutz.dao.impl.DaoSupport$2.invoke(DaoSupport.java:181)
    at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:43)
    ... 4 more
Caused by: java.sql.SQLException: 不能将值 NULL 插入列 
'CategoryID',表 
'Factory.dbo.ArticleCategorys';列不允许有空值。INSERT 失败。
    at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
    at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2820)
    at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258)
    at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:632)
    at net.sourceforge.jtds.jdbc.JtdsStatement.processResults(JtdsStatement.java:584)
    at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQL(JtdsStatement.java:546)
    at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.execute(JtdsPreparedStatement.java:558)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
    at org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:179)
    at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:125)
    ... 6 more

Original comment by wangying...@gmail.com on 1 Jun 2011 at 1:46

GoogleCodeExporter commented 9 years ago
可以把pojo里的View换成数据库的视图
@View ("Factory.dbo.ViewArticleCategorys") 
这样就可以正常执行了。

Original comment by wangying...@gmail.com on 1 Jun 2011 at 1:48

GoogleCodeExporter commented 9 years ago
更新也会出错的。
会把@Readonly的字段更新的
UPDATE  Factory.dbo.ArticleCategorys SET categoryName='11',parentID=65  WHERE 
generalID=223 AND categoryID=1111
Caused by: java.sql.SQLException: 列名 'categoryName' 无效。

Original comment by wangying...@126.com on 1 Jun 2011 at 8:48

GoogleCodeExporter commented 9 years ago
调用的代码能贴出来看看吗?
我看看我本地能不能重现

Original comment by zozoh...@gmail.com on 1 Jun 2011 at 1:43

GoogleCodeExporter commented 9 years ago
看来 SqlServer2005 的 JdbcExpert 有问题
抽象的类里的  setupEntityField  可能不适合 SqlServer

Original comment by zozoh...@gmail.com on 3 Jun 2011 at 9:14

GoogleCodeExporter commented 9 years ago
dwwwcn   18:00:01
ArticleCategorys ac = dao.fetchx(ArticleCategorys.class, 1, 1); 
还是会报错的
三个字儿   18:07:20
恩,这就对了,看来是 Entity 解析的问题
三个字儿   18:07:47
解析的时候,没认为这个是复合主键

Original comment by zozoh...@gmail.com on 3 Jun 2011 at 10:09

GoogleCodeExporter commented 9 years ago
哦? 我看看去

Original comment by wendal1985@gmail.com on 3 Jun 2011 at 11:57

GoogleCodeExporter commented 9 years ago
createResultSetMetaSql的问题吧?

Original comment by wendal1985@gmail.com on 3 Jun 2011 at 12:00

GoogleCodeExporter commented 9 years ago
啥问题?

Original comment by zozoh...@gmail.com on 3 Jun 2011 at 1:21

GoogleCodeExporter commented 9 years ago
因为现在获取的数据库字段信息,是通过createResultSetMetaSql方法
的SQL语句获取的.而createResultSetMetaSql里面写的SQL是select语句,se
lect view的语句.故,如果view没有某一字段,Nutz就无视某些字段?

Original comment by wendal1985@gmail.com on 6 Jun 2011 at 7:09

GoogleCodeExporter commented 9 years ago
不会无视, createResultSetMetaSql 
只不过是想拿到当前实体在数据库中真实得信息
如果没有这个字段或者数据表,则以注解定义为准。

这样设计是考虑两种使用方式,一种是使用者已经手动建立��
�数据表,一种时使用者想通过 Nutz 来建立数据表
这个策略在这两种情况下都能最合理得工作

总之,你声明了一个实体字段, Nutz.Dao 一定不会无视它得

Original comment by zozoh...@gmail.com on 6 Jun 2011 at 11:35