2017-03-02 23 views
1

JPaを使用してmysqlデータベースに接続しようとしています。私はpersistance.xml生成したJPAを使用してspringとinteliJを使用してデータベースに接続する

:それをすることによって

<?xml version="1.0" encoding="UTF-8"?> 
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0"> 

    <persistence-unit name="NewPersistenceUnit"> 
     <class>com.example.TorkoEntity</class> 
     <properties> 
      <property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/tornoci"/> 
      <property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
      <property name="eclipselink.jdbc.user" value="matej"/> 
      <property name="eclipselink.jdbc.password" value="123456"/> 
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/tornoci"/> 
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> 
      <property name="hibernate.connection.username"/> 
      <property name="hibernate.connection.password"/> 
      <property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/tornoci"/> 
      <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/> 
      <property name="openjpa.ConnectionUserName"/> 
      <property name="openjpa.ConnectionPassword"/> 
      <property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/tornoci"/> 
      <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/> 
      <property name="toplink.jdbc.user"/> 
      <property name="toplink.jdbc.password"/> 
     </properties> 
    </persistence-unit> 
</persistence> 

とalstoは、生成されたPOJOクラスをintelij:

@Entity 
@Table(name = "torko", schema = "tornoci", catalog = "") 
public class TorkoEntity { 
    private int id; 
    private String meno; 
    TorkoEntity(){} 
    @Id 
    @Column(name = "Id", nullable = false) 
    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 

    @Basic 
    @Column(name = "meno", nullable = false, length = 250) 
    public String getMeno() { 
     return meno; 
    } 

    public void setMeno(String meno) { 
     this.meno = meno; 
    } 

    @Override 
    public boolean equals(Object o) { 
     if (this == o) return true; 
     if (o == null || getClass() != o.getClass()) return false; 

     TorkoEntity that = (TorkoEntity) o; 

     if (id != that.id) return false; 
     if (meno != null ? !meno.equals(that.meno) : that.meno != null) return false; 

     return true; 
    } 

    @Override 
    public int hashCode() { 
     int result = id; 
     result = 31 * result + (meno != null ? meno.hashCode() : 0); 
     return result; 
    } 
} 

私はエンティティマネージャを作成し、そこから結果を取得したいています

public class manager { 
    @PersistenceContext 
    EntityManager em; 
    manager(){} 
    public List<TorkoEntity> getHim(){ 
     Query q = em.createNativeQuery("SELECT * FROM TorkoEntity", TorkoEntity.class); 
     return q . getResultList(); 
    } 
} 

およびルート:

@RestController 
public class test { 

    @RequestMapping(value = "hi" , method = RequestMethod.GET) 
    public String testt(){ 
     manager m = new manager(); 
     List<TorkoEntity> t = m.getHim(); 
     return t.get(0).getMeno(); 
    } 
} 

しかし、私は、ローカルホストに移動するとき:8080/HI

それはコンソールで

Whitelabel Error Page 

This application has no explicit mapping for /error, so you are seeing this as a fallback. 

Thu Mar 02 19:21:15 CET 2017 
There was an unexpected error (type=Internal Server Error, status=500). 
No message available 
ブラウザで

java.lang.NullPointerException: null 
    at com.example.manager.getHim(manager.java:17) ~[classes/:na] 
    at com.example.test.testt(test.java:19) ~[classes/:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_65] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65] 
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65] 
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) ~[tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) ~[tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230) ~[tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.6.RELEASE.jar:4.3.6.RELEASE] 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192) ~[tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165) ~[tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_65] 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_65] 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.11.jar:8.5.11] 
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_65] 

をスローします。 私はこの問題を引き起こす可能性がありますどのような見当がつかない、すべてのヘルプは非常に

答えて

0

を高く評価している問題は、あなたがこの部分で

manager m = new manager(); 

を作成していることをあなたがにあなたのコントローラーにあなたのマネージャークラスをautowireする必要があり、のようですそれを機能させる。あなたのプログラムをデバッグする場合、そのマネージャーがnullであることがわかります。

良い答えはここにある: Why is my Spring @Autowired field null?

+0

は、あなたがそれを証明してくださいだろうか?私はちょっと失ったような気がします。マネージャー宣言の前にAutowired anotationを追加し、マネージャークラスのサービスアノテーションを追加しましたが、エラーは同じです。 – Darlyn

+0

私の最初の答えはちょっと間違っていました。昨日同じ問題があった。私の解決策:http://stackoverflow.com/questions/42530667/custom-queries-in-spring-with-jpa?noredirect=1#comment72198517_42530667 – JamesLinux

+0

私の設定は、リポジトリを作成しようとすると少し違う、春モジュールを見つけることができません – Darlyn

関連する問題