NutzCN Logo
问答 nutz @Id疑问
发布于 3066天前 作者 qq_305db03e 4239 次浏览 复制 上一个帖子 下一个帖子
标签: dao

@Table("SYSTEM_MENU")
public class Menu {
@Id
private Long id;

连接的是pg数据库,数据库不支持自增id,如果数据库不支持自增id,插入数据就会提示主键重复。没有以前那种max+1的方法!
17 回复

nutz建的表? max+1只是在mysql类的数据库下不使用
自豪地采用 NutzCN ionic

只要是nutz建的表,@Id都能跑才对
自豪地采用 NutzCN ionic

而且pg支持自增呢 seq序列,nutz建的字段就是这个
自豪地采用 NutzCN ionic

@Table("SYSTEM_MENU")
public class Menu {
@Id
private Long id;
@Column
@ColDefine(type = ColType.VARCHAR, width = 20)
private String name;
@Column
@ColDefine(type = ColType.VARCHAR, width = 500)
private String url;
@Column
@ColDefine(type = ColType.VARCHAR, width = 500)
private String description;
@Column
private Long lft;
@Column
private Long rgt;
@Column
private Long parentid;
@Column
private Long permissionId;

2015-10-12 15:8:0.926 DEBUG [http-bio-8080-exec-6] [whenException] Obj = [com.dolplay.dolpbase.system.service.MenuService$$NUTZAOP] , Throwable = org.nutz.dao.DaoException: !Nutz SQL Error: 'INSERT INTO SYSTEM_MENU(name,url,description,lft,rgt,parentid,permissionId) VALUES(?,?,?,?,?,?,?)
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|----|----|----|------|------|----|------|
| 11 | 11 | 11 | NULL | NULL | 25 | NULL |
For example:> "INSERT INTO SYSTEM_MENU(name,url,description,lft,rgt,parentid,permissionId) VALUES('11','11','11',NULL,NULL,25,NULL) "'
PreparedStatement:
'INSERT INTO SYSTEM_MENU(name,url,description,lft,rgt,parentid,permissionId) VALUES(?,?,?,?,?,?,?) '
CaseMessage=ERROR: duplicate key value violates unique constraint "system_menu_pkey"
详细:Key (id)=(1) already exists. , Method = public com.dolplay.dolpbase.common.domain.AjaxResData com.dolplay.dolpbase.system.service.MenuService.CUDMenu(java.lang.String,java.lang.String,com.dolplay.dolpbase.system.domain.Menu,java.lang.Long) , args = [add,null,com.dolplay.dolpbase.system.domain.Menu@5c1e94f1,25]
2015-10-12 15:8:0.926 DEBUG [http-bio-8080-exec-6] [afterInvoke] Obj = [com.dolplay.dolpbase.system.service.MenuService$$NUTZAOP] , Return = null , Method = public com.dolplay.dolpbase.common.domain.AjaxResData com.dolplay.dolpbase.system.service.MenuService.CUDMenu(java.lang.String,java.lang.String,com.dolplay.dolpbase.system.domain.Menu,java.lang.Long) , args = [add,null,com.dolplay.dolpbase.system.domain.Menu@5c1e94f1,25]
2015-10-12 15:8:0.926 WARN [http-bio-8080-exec-6] Error@/system/menu/editRow/25 :
org.nutz.dao.DaoException: !Nutz SQL Error: 'INSERT INTO SYSTEM_MENU(name,url,description,lft,rgt,parentid,permissionId) VALUES(?,?,?,?,?,?,?)
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|----|----|----|------|------|----|------|
| 11 | 11 | 11 | NULL | NULL | 25 | NULL |
For example:> "INSERT INTO SYSTEM_MENU(name,url,description,lft,rgt,parentid,permissionId) VALUES('11','11','11',NULL,NULL,25,NULL) "'
PreparedStatement:
'INSERT INTO SYSTEM_MENU(name,url,description,lft,rgt,parentid,permissionId) VALUES(?,?,?,?,?,?,?) '
CaseMessage=ERROR: duplicate key value violates unique constraint "system_menu_pkey"
详细:Key (id)=(1) already exists.
at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:97)
at org.nutz.dao.impl.DaoSupport$DaoExec.invoke(DaoSupport.java:264)
at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:64)
at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:219)
at org.nutz.dao.impl.EntityOperator.exec(EntityOperator.java:50)
at org.nutz.dao.impl.NutDao.insert(NutDao.java:144)
at com.dolplay.dolpbase.system.service.MenuService.CUDMenu(MenuService.java:234)
at com.dolplay.dolpbase.system.service.MenuService$$NUTZAOP._aop_invoke(MenuService.java:1)
at org.nutz.aop.InterceptorChain.invoke(InterceptorChain.java:77)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:57)
at org.nutz.aop.interceptor.LoggingMethodInterceptor.filter(LoggingMethodInterceptor.java:48)
at org.nutz.aop.InterceptorChain.doChain(InterceptorChain.java:60)
at com.dolplay.dolpbase.system.service.MenuService$$NUTZAOP.CUDMenu(MenuService.java)
at com.dolplay.dolpbase.system.module.MenuModule.editRow(MenuModule.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:40)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:113)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:40)
at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:183)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "system_menu_pkey"
详细:Key (id)=(1) already exists.
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2182)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1911)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:173)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:616)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:466)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:459)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:476)
at org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:266)
at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:81)

表不是nutz建的吧?id不是定义为seq,所以不行咯
自豪地采用 NutzCN ionic

    if (null != initDolpTables && initDolpTables.toUpperCase().equals("TRUE")) {
       logger.info("初始化Dolp数据表");
       // 批量建表
       for (Class<?> klass : Scans.me().scanPackage("com.dolplay.dolpbase.system.domain")) {
         if (klass.getAnnotation(Table.class) != null) {
          dao.create(klass, true);
         }
       }

       表 是,只是自己插入了部分数据,这样就不行拉?

自己插入了部分数据? 当前的表结构是怎样的?
自豪地采用 NutzCN ionic

-- Table: system_menu

-- DROP TABLE system_menu;

CREATE TABLE system_menu
(
id serial NOT NULL,
name character varying(20),
url character varying(500),
description character varying(500),
lft numeric(16,0),
rgt numeric(16,0),
parentid numeric(16,0),
permissionid numeric(16,0),
CONSTRAINT system_menu_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE system_menu
OWNER TO postgres;

数据 前面初始化数据 是用sql脚本插入的,现在用程序来添加数据就会提示主键重复!

哦,我貌似知道你的意思了。你插入了一些指定id的数据,但seq依然指向老的位置,所以呢,seq自增的时候就报错了。所以你需要@Id(auto=false) @Prev(@Sql("select max........."))
自豪地采用 NutzCN ionic

@wendal
@Column("topicid") //@id的文档说不需要在用这个注解
@Id
@ColDefine(type = ColType.INT)
private Integer topicid;
id怎么重名名
topicid是数据库是原来系统已经在使用的字段名 主键 。。。nutz默认用id 这么设置到 topicid上

@sunhai1988 nutz默认用id是啥意思?

@Id标哪里就是数值主键,具体字段名是属性名,或@Column的值,没有默认id一说

来自炫酷的 NutzCN

```
public Integer getTopicid() {
return topicid;
}

public void setTopicid(Integer topicid) {
    this.topicid = topicid;
}



``` Caused by: java.lang.NoSuchMethodException: Fail to find getter for [cn.wizzer.modules.back.*.models.Topic]->[id]

@sunhai1988 完整报错信息

来自炫酷的 NutzCN

@wendal 我知道怎么改了。。 估计nutz在修改的时候取默认用的id !

    @Column("topicid")
    @Id
    @ColDefine(type = ColType.INT)
    private Integer id;
    

修正代码后成功:在java bean 里面 这个需要用“id” 数据库里面可以用 @Column("topicid") 映射。。

@sunhai1988 说了不是默认取id,是默认去属性名

来自炫酷的 NutzCN

恩 我明天再研究研究 多谢

添加回复
请先登陆
回到顶部