Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

Problems linking mysql database with java based web app

I am using Spring Data to pull and control data from a mysql database. I have been having problems putting this data into a table. I am using Vaadin and following these tutorials:

https://vaadin.com/learn/tutorials/modern-web-apps-with-spring-boot-and-vaadin/spring-boot-backend-database-jpa-repositories-services

(but am trying to adapt for my own purposes). The tutorial creates an AbstractEntity from which other entities extend. From what I have worked out, this is some form of entity manager which I believe attempts to distinguish unique entities. As I was just attempting to see how all of these objects fit together, I decided to create only one entity and extend it from this AbstractEntity named Stock. This Stock Class is defined as follows:

package com.stockmachine.v2.backend.entity;

import javax.persistence.*;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

@Entity
@Table (name="stock_info")
public class Stock extends AbstractEntity implements Cloneable {
    @Id
    private String Ticker;
    
    private String Region;

    public String getTicker() {
        return Ticker;
    }
    public void setTicker(String ticker) {
        Ticker = ticker;
    }
    
    public String getRegion() {
        return Region;
    }
    public void setRegion(String region) {
        Region = region;
    }
    

}

This links to a table in my mysql database named "stock_info", the table has more than two columns but I was only testing, so I decided to only create variables for two of these columns, 'Region' and 'Ticker'. This is just a database with a list of stock tickers and has some information about them, one of the bits of information being the region.

The AbstractEntity is defined as follows:

package com.stockmachine.v2.backend.entity;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public abstract class AbstractEntity {
}

I also have the following StockService Service defined as follows:

import com.stockmachine.v2.backend.entity.Stock;
import com.stockmachine.v2.backend.repository.StockRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

@Service 
public class StockService {
    private static final Logger LOGGER = Logger.getLogger(StockService.class.getName());
    private StockRepository stockRepository;

    public StockService(StockRepository stockRepository) { 
        this.stockRepository = stockRepository;
    }

    public List<Stock> findAll() {
        return stockRepository.findAll();
    }

    public long count() {
        return stockRepository.count();
    }

    public void delete(Stock stock) {
        stockRepository.delete(stock);
    }

    public void save(Stock stock) {
        if (stock == null) { 
            LOGGER.log(Level.SEVERE,
                    "Contact is null. Are you sure you have connected your form to the application?");
            return;
        }
        stockRepository.save(stock);
    }
}

and repository:

package com.stockmachine.v2.backend.repository;

import com.stockmachine.v2.backend.entity.Stock;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface StockRepository extends JpaRepository<Stock, String> {

}

And the main view ui is:

package com.stockmachine.v2.ui;

import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import com.stockmachine.v2.backend.entity.Stock;
import com.vaadin.flow.component.grid.Grid;
import com.stockmachine.v2.backend.service.StockService;

@Route("") 
public class MainView extends VerticalLayout {
    private StockService stockService;
    private Grid<Stock> grid = new Grid<>(Stock.class); 

    public MainView(StockService stockService) {
        addClassName("list-view"); 
        setSizeFull(); 
        configureGrid(); 
        
        add(grid); 
        updateList();
    }
   

  private void configureGrid() {
    grid.addClassName("stock-grid");
    grid.setSizeFull();
    grid.setColumns("ticker","region"); 

   }
  private void updateList() {
      grid.setItems(stockService.findAll());
   }
}

From what I understand (may be completely wrong), the main view will create an instance of the service class StockService which creates an instance of the StockRepository which gets various parameters from the Stock class which is linked to the database table referenced with @Table.This throws the following errors:


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.stockmachine.v2.ui.MainView': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.stockmachine.v2.ui.MainView]: Constructor threw exception; nested exception is java.lang.NullPointerException: Cannot invoke "com.stockmachine.v2.backend.service.StockService.findAll()" because "this.stockService" is null
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:313) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:294) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1356) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1203) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:307) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at com.vaadin.flow.spring.SpringInstantiator.getOrCreate(SpringInstantiator.java:117) ~[vaadin-spring-12.3.2.jar:na]
    at com.vaadin.flow.di.Instantiator.createRouteTarget(Instantiator.java:160) ~[flow-server-2.4.3.jar:2.4.3]
    at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.lambda$getRouteTarget$1(AbstractNavigationStateRenderer.java:135) ~[flow-server-2.4.3.jar:2.4.3]
    at java.base/java.util.Optional.orElseGet(Optional.java:362) ~[na:na]
    at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.getRouteTarget(AbstractNavigationStateRenderer.java:134) ~[flow-server-2.4.3.jar:2.4.3]
    at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.sendBeforeEnterEventAndPopulateChain(AbstractNavigationStateRenderer.java:518) ~[flow-server-2.4.3.jar:2.4.3]
    at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.createChainIfEmptyAndExecuteBeforeEnterNavigation(AbstractNavigationStateRenderer.java:498) ~[flow-server-2.4.3.jar:2.4.3]
    at com.vaadin.flow.router.internal.AbstractNavigationStateRenderer.handle(AbstractNavigationStateRenderer.java:230) ~[flow-server-2.4.3.jar:2.4.3]
    at com.vaadin.flow.router.Router.handleNavigation(Router.java:249) ~[flow-server-2.4.3.jar:2.4.3]
    at com.vaadin.flow.router.Router.navigate(Router.java:220) ~[flow-server-2.4.3.jar:2.4.3]
    at com.vaadin.flow.router.Router.navigate(Router.java:186) ~[flow-server-2.4.3.jar:2.4.3]
    at com.vaadin.flow.router.Router.initializeUI(Router.java:93) ~[flow-server-2.4.3.jar:2.4.3]
    at com.vaadin.flow.server.BootstrapHandler.createAndInitUI(BootstrapHandler.java:1526) ~[flow-server-2.4.3.jar:2.4.3]
    at com.vaadin.flow.server.BootstrapHandler.synchronizedHandleRequest(BootstrapHandler.java:475) ~[flow-server-2.4.3.jar:2.4.3]
    at com.vaadin.flow.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40) ~[flow-server-2.4.3.jar:2.4.3]
    at com.vaadin.flow.server.VaadinService.handleRequest(VaadinService.java:1545) ~[flow-server-2.4.3.jar:2.4.3]
    at com.vaadin.flow.server.VaadinServlet.service(VaadinServlet.java:247) ~[flow-server-2.4.3.jar:2.4.3]
    at com.vaadin.flow.spring.SpringServlet.service(SpringServlet.java:111) ~[vaadin-spring-12.3.2.jar:na]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:712) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:459) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:352) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:312) ~[tomcat-embed-core-9.0.37.jar:9.0.37]
    at org.springframework.web.servlet.mvc.ServletForwardingController.handleRequestInternal(ServletForwardingController.java:141) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:177) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:52) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.37.jar:4.0.FR]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at javax.servlet.http.HttpServlet.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First problem is that Stock was defining two ids (id form AbstractEntity and Ticker) and is not implementing Serializable.

For the second issue...just add this.stockService = stockService in MainView constructor


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...