主题:BeetlSQL在POJO类中定义@AssignID,@UpdateTime无法生成SQL语句

seale 8天前 24

POJO对象,定义了@AssignID("uuid"),@UpdateTime

@Getter@Setter
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@Table(name = "user")
public class User implements Serializable {    
private static final long serialVersionUID = 5677565502925870794L;    
@AssignID("uuid")    
private String id ;    
private String username;    
private String password;    
@UpdateTime    
private Date createTime;

}

测试的Controller

@RestController

public class TestController {    
@Autowired    
SQLManager sqlManager;    
@RequestMapping("/test")    
public String test(){        
sqlManager.addIdAutonGen("uuid", new IDAutoGen() {            
@Override            
public Object nextID(String params) {                
String uuid = UUID.randomUUID().toString().replaceAll("-","").toUpperCase();                System.out.println(uuid);                
return uuid;            
}        
});        
User user = new User();        
user.setUsername("test")                
.setPassword("123456");        
sqlManager.insert(User.class,user);  
return "";    
}}


生成的SQL代码

┏━━━━━ Debug [user._gen_insert] ━━━

 ┣ SQL: insert into `user` (`id`,`username`,`password`) VALUES (?,?,?)

 ┣ 参数: [null, test, 123456]

 ┣ 位置: com.eendtech.beetltest.controller.TestController.test(TestController.java:39) 

┣ 时间: 116ms 

┣ 更新: [1] 

┗━━━━━ Debug [user._gen_insert] ━━━

使用的Beetl版本和SpringBoot版本

<beetl.version>1.2.18.RELEASE</beetl.version>
<SpringBoot.version>2.2.0.RELEASE</SpringBoot.version>


附:同时我也尝试不使用lombok,直接手动生成getter,setter方法之后在getter方法上面加注解,但是依旧没有正常生成uuid和updateTime注解的SQL语句

seale 8天前

在给数据库表的ID加上主键后报的是空指针异常

java.lang.NullPointerException: null at org.beetl.core.fun.PojoMethodInvoker.set(PojoMethodInvoker.java:76) ~[beetl-3.0.15.RELEASE.jar:na] at org.beetl.sql.core.kit.BeanKit.setBeanProperty(BeanKit.java:190) ~[beetlsql-2.12.16.RELEASE.jar:na] at org.beetl.sql.core.SQLScript.addParaIfAssignId(SQLScript.java:1046) ~[beetlsql-2.12.16.RELEASE.jar:na] at org.beetl.sql.core.SQLScript.insert(SQLScript.java:151) ~[beetlsql-2.12.16.RELEASE.jar:na] at org.beetl.sql.core.SQLManager.generalInsert(SQLManager.java:1163) ~[beetlsql-2.12.16.RELEASE.jar:na] at org.beetl.sql.core.SQLManager.insert(SQLManager.java:1111) ~[beetlsql-2.12.16.RELEASE.jar:na] at org.beetl.sql.core.SQLManager.insert(SQLManager.java:1137) ~[beetlsql-2.12.16.RELEASE.jar:na] at com.eendtech.beetltest.controller.TestController.test(TestController.java:39) ~[classes/:na] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181] at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:888) ~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793) ~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.27.jar:9.0.27] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.0.RELEASE.jar:5.2.0.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.27.jar:9.0.27] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27] at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27] at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27] at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526) [tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861) [tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579) [tomcat-embed-core-9.0.27.jar:9.0.27] at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.27.jar:9.0.27] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181] at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.27.jar:9.0.27] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]

seale 8天前

之后尝试更改返回对象Object->String

sqlManager.addIdAutonGen("uuid", new IDAutoGen() {
            @Override
            public String nextID(String params) {
                String uuid = UUID.randomUUID().toString().replaceAll("-","").toUpperCase();
                System.out.println("UUID:"+uuid);
                return uuid;
            }
        });

能够正常打印System.out.println

UUID:439091B7C14340E095071BEA59F8EEB5

但还是会报

java.lang.NullPointerException: null
seale 7天前

最后发现确实是user对象并没有被SQLManager的自定义ID生成器赋值,不知道如何解决。

joelli 5天前

@Accessors(chain = true) 是什么意思,这生成非标准的JavaBean,会找不到“属性”的

  • seale :这个只是链式调用,不影响的,@UpdateTime这个注解使用没有问题,就自定义生成ID有问题(3天前)