Biljeznica.java:
@Entity
@Table(name = "biljeznica")
public class Biljeznica implements Serializable {
/**
*
*/
private static final long serialVersionUID = -5589212916227508153L;
@Id
@Column(name= "naziv")
private String naziv;
@Column(name= "opis")
private String opis;
public Biljeznica(){}
// getters and setters
Korisnik.java:
@Entity
@Table(name = "user")
public class Korisnik extends NovaBiljeskaForm implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3476436475286629836L;
@Id
@Column(name= "id")
@GeneratedValue
private int id;
@Column(name= "ime")
private String ime;
@Column(name= "prezime")
private String prezime;
@Column(name= "username")
private String nick;
public Korisnik(){}
//getters and setters
Biljeska.java:
@Entity
@Table(name = "biljeska")
public class Biljeska implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3706596321179671012L;
@Id
@Column(name= "id")
@GeneratedValue
int id;
@Column(name = "naslov")
private String naslov;
@Column(name = "tekst")
private String tekst;
@ManyToOne (cascade = CascadeType.ALL)
@JoinColumn(name ="korisnik")
private Korisnik korisnik;
@ManyToOne (cascade = CascadeType.ALL)
@JoinColumn(name = "biljeznica")
private Biljeznica biljeznica;
public Biljeska(){}
h2架构:
CREATE TABLE user (
id INT(11) IDENTITY PRIMARY KEY,
ime VARCHAR(50) NOT NULL,
prezime VARCHAR(50) NOT NULL,
username VARCHAR(45),
password VARCHAR(100) NOT NULL ,
enabled TINYINT NOT NULL DEFAULT 1
);
CREATE TABLE biljeznica (
naziv VARCHAR(50) PRIMARY KEY,
opis VARCHAR(100) NOT NULL
);
CREATE TABLE biljeska (
id INT(11) IDENTITY PRIMARY KEY,
naslov VARCHAR(100),
tekst VARCHAR(1000),
korisnik VARCHAR(45) NOT NULL,
biljeznica VARCHAR(50) NOT NULL,
FOREIGN KEY (korisnik) REFERENCES user (username),
FOREIGN KEY (biljeznica) REFERENCES biljeznica (naziv)
);
我通过test-data.sql插入到架构中的数据:
INSERT INTO user(ime, prezime,username,password,enabled)
VALUES ('Ivo','Ivić','iivic','$2a$10$VQoRdaQK0KzAl4QoBzfZleRBcHitGS5jRA771ilGdL2DvkVTLDqSe', true);
INSERT INTO user(ime, prezime,username,password,enabled)
VALUES ('Marko','Markić','mmarkic','$2a$10$VQoRdaQK0KzAl4QoBzfZleRBcHitGS5jRA771ilGdL2DvkVTLDqSe', true);
INSERT INTO user(ime, prezime,username,password,enabled)
VALUES ('Pero','Perić','pperic','$2a$10$VQoRdaQK0KzAl4QoBzfZleRBcHitGS5jRA771ilGdL2DvkVTLDqSe', true);
INSERT INTO user(ime, prezime,username,password,enabled)
VALUES ('Adminko','Adminić','admin','$2a$10$VQoRdaQK0KzAl4QoBzfZleRBcHitGS5jRA771ilGdL2DvkVTLDqSe', true);
INSERT INTO user_role (username, role)
VALUES ('iivic', 'ROLE_USER');
INSERT INTO user_role (username, role)
VALUES ('mmarkic', 'ROLE_USER');
INSERT INTO user_role (username, role)
VALUES ('pperic', 'ROLE_USER');
INSERT INTO user_role (username, role)
VALUES ('admin', 'ROLE_USER');
INSERT INTO user_role (username, role)
VALUES ('admin', 'ROLE_ADMIN');
INSERT INTO biljeznica(naziv, opis)
VALUES('Web aplikacije u Javi', 'Bilješke s predavanja iz kolegija web aplikacije u
Javi');
INSERT INTO biljeznica(naziv, opis)
VALUES('Razvoj aplikacija na Android platformi', 'Bilješke s predavanja kolegija razvoj
aplikacija na Android platformi');
INSERT INTO biljeznica(naziv, opis)
VALUES('Napredne teme računalnih mreža', 'Bilješke s predavanja napredne teme
računalnih mreža');
INSERT INTO biljeska(naslov, tekst, korisnik, biljeznica)
VALUES('Testni naslov', 'Testni tekst', 'iivic', 'Web aplikacije u Javi');
INSERT INTO biljeska(naslov, tekst, korisnik, biljeznica)
VALUES('Moj naslov', 'Moj tekst', 'pperic', 'Web aplikacije u Javi');
存储库类中我想返回Biljeska对象列表的方法:
@Autowired
public HibernateBiljeskaRepository(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
private Session currentSession() {
return sessionFactory.getCurrentSession();
}
public List<Biljeska> findAll() {
return currentSession().createQuery("select b from Biljeska b, Biljeznica bi, Korisnik k where b.korisnik = k and b.biljeznica = bi",Biljeska.class).getResultList();
}
在findAll()方法
的返回语句中发生异常错误消息我得到了:
Apr 28, 2017 1:19:01 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 22018, SQLState: 22018
Apr 28, 2017 1:19:01 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Data conversion error converting "iivic"; SQL statement:
select biljeska0_.id as id1_0_, biljeska0_.biljeznica as biljezni4_0_, biljeska0_.korisnik as korisnik5_0_, biljeska0_.naslov as naslov2_0_, biljeska0_.tekst as tekst3_0_ from biljeska biljeska0_ cross join biljeznica biljeznica1_ cross join user korisnik2_ where biljeska0_.korisnik=korisnik2_.id and biljeska0_.biljeznica=biljeznica1_.naziv [22018-193]
Apr 28, 2017 1:19:01 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/labos1] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not extract ResultSet] with root cause
java.lang.NumberFormatException: For input string: "iivic"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at java.lang.Long.parseLong(Unknown Source)
at org.h2.value.Value.convertTo(Value.java:922)
at org.h2.mvstore.db.MVPrimaryIndex.getKey(MVPrimaryIndex.java:338)
at org.h2.mvstore.db.MVDelegateIndex.find(MVDelegateIndex.java:69)
at org.h2.index.BaseIndex.find(BaseIndex.java:128)
at org.h2.index.IndexCursor.find(IndexCursor.java:169)
at org.h2.table.TableFilter.next(TableFilter.java:467)
at org.h2.table.TableFilter.next(TableFilter.java:537)
at org.h2.command.dml.Select.queryFlat(Select.java:542)
at org.h2.command.dml.Select.queryWithoutCache(Select.java:655)
at org.h2.command.dml.Query.query(Query.java:341)
at org.h2.command.dml.Query.query(Query.java:309)
at org.h2.command.dml.Query.query(Query.java:36)
at org.h2.command.CommandContainer.query(CommandContainer.java:110)
at org.h2.command.Command.executeQuery(Command.java:201)
at org.h2.jdbc.JdbcPreparedStatement.executeQuery(JdbcPreparedStatement.java:110)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:71)
at org.hibernate.loader.Loader.getResultSet(Loader.java:2123)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1911)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1887)
at org.hibernate.loader.Loader.doQuery(Loader.java:932)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349)
at org.hibernate.loader.Loader.doList(Loader.java:2615)
at org.hibernate.loader.Loader.doList(Loader.java:2598)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2430)
at org.hibernate.loader.Loader.list(Loader.java:2425)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:502)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:375)
at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:216)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1481)
at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1441)
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1410)
at org.hibernate.query.Query.getResultList(Query.java:146)
at hr.tvz.hibernate.HibernateBiljeskaRepository.findAll(HibernateBiljeskaRepository.java:58)
at hr.tvz.hibernate.HibernateBiljeskaRepository$$FastClassBySpringCGLIB$$d420114f.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656)
at hr.tvz.hibernate.HibernateBiljeskaRepository$$EnhancerBySpringCGLIB$$d7521f20.findAll(<generated>)
at hr.tvz.controleri.NovaBiljeskaController.showSpremljeneBiljeske(NovaBiljeskaController.java:215)
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.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:158)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:341)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:799)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1455)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
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)
我真的很困惑,我不明白为什么我会收到这个错误... 提前谢谢!
编辑:
谢谢Alex
解决方案:将Biljeska属性更改为
@ManyToOne (fetch = FetchType.EAGER)
@JoinColumn(name ="korisnik", referencedColumnName="username")
private Korisnik korisnik;
@ManyToOne (fetch = FetchType.EAGER)
@JoinColumn(name = "biljeznica", referencedColumnName="naziv")
private Biljeznica biljeznica;
和hql到:
"select b from Biljeska b"
答案 0 :(得分:1)
在SQL代码中,您为biljeska.korisnik
提供了user.username
的外键。但是在Java代码中,你没有指定外键引用的Korisnik
类的哪一列,所以它试图引用主键,这是一个整数,这就是你得到{{1}的原因。 }。
您必须使用NumberFormatException
中的JoinColumn.referencedColumnName
来引用Biljeska.korisnik
列。