Can Spring JdbcTemplate be used with Vaadin?

Hi,

I’m trying to put together a simple example that uses Vaadin and Spring Boot and utilizes the database through Spring mechanisms.

The Vaadin TableQuery requires a connection pool argument. But Spring handles this behind the scenes. Is there a way to connect these pieces?

Using 7.3.10 and Spring Boot 1.2.1

Thanks.

Les

Short answer: Yes. You just have to fill the dataSources of your vaadin components with the result of your queries. Do you use JPA for your entities? So you can use the JPA container from vaadin:
https://vaadin.com/book/vaadin7/-/page/jpacontainer.html

Thanks Wolfgang. I finally did figure it out by taking the results and putting them into a BeanItemContainer and then setting the data source for the Table to the item container.

I’m looking at non-JPA specific solutions since we have some legacy stuff that uses a home-grown implementation to access the database.

Les

I am struggling for many days with this as well, is there an example which shows a simple application which uses Vaadin, Spring and JDBC working.

I can’t give you a complete example right now but I have some good news for you:
https://vaadin.com/blog/-/blogs/vaadin-spring-1-0-0-is-out-

With the utility of this add-on it should be no problem keep your application running. All you need is a correct configured spring application, vaadin with vaadin-spring and you have to fulfill the interfaces of the bean item containiers of you choice.

Hi,
I’m also struggling with that problem: When deploying my TestCode I’m getting a NPE for the autowired PersonService on that line:

if ( personService.addPerson(person) > 0){ Here’s the UI class:

[code]
@SpringUI
public class EquipmentDBUI extends UI {

Logger logger = LoggerFactory.getLogger(EquipmentDBUI.class);

  @Autowired
  PersonService personService;

@WebServlet(value = "/*", asyncSupported = true)
@VaadinServletConfiguration(productionMode = false, ui = EquipmentDBUI.class)
public static class Servlet extends VaadinServlet {
}

@Override
protected void init(VaadinRequest request) {

    final VerticalLayout layout = new VerticalLayout();
    layout.setMargin(true);
    setContent(layout);

        
    Person person = new Person();
    person.setFirstName("FName");
    person.setLastName("LName");
    person.setAge(20);
    person.setPlace("Place");

    if ( personService.addPerson(person) > 0){
      logger.info("Person saved successfully");
    }

    
    String t="";
    for(Person p : personService.getAllPerson()){
      t=t+" "+(p.toString());
    }
        
    layout.addComponent(new Label("Database Content: "+t ));
            
}

}
[/code]This is the PersonService Class:

[code]
@Service
public class PersonService {

@Autowired
private JdbcTemplate jdbcTemplate;

public int addPerson(Person person){
jdbcTemplate.execute(“DROP TABLE person IF EXISTS”);
jdbcTemplate.execute("CREATE TABLE person( first_name VARCHAR(150), "
+ “last_name VARCHAR(150), age INTEGER, place VARCHAR(100)”);

String sql = "INSERT INTO person(first_name, last_name, age, place) VALUES(?,?,?,?)";
return jdbcTemplate.update(sql, person.getFirstName(),
    person.getLastName(), person.getAge(), person.getPlace());

}

public List getAllPerson(){
return jdbcTemplate.query(“SELECT * FROM person”, new RowMapper(){

  public Person mapRow(ResultSet rs, int arg1) throws SQLException {
    Person p = new Person();
    p.setAge(rs.getInt("age"));
    p.setFirstName(rs.getString("first_name"));
    p.setLastName(rs.getString("last_name"));
    p.setPlace(rs.getString("place"));
    return p;
  }

});

}
}
[/code]the Person class:

[code]
public class Person {
private String firstName;
private String lastName;
private int age;
private String place;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}

public String toString(){
StringBuilder builder = new StringBuilder();
builder.append(this.getFirstName())
.append(", “)
.append(this.getLastName())
.append(”, “)
.append(this.getPlace())
.append(”, ")
.append(this.getAge());

return builder.toString();

}

}
[/code]the application.properties:

spring.datasource.platform=hsqldb

and the pom.xml

[code]

<?xml version="1.0" encoding="UTF-8"?>


4.0.0

<groupId>equipmentdb</groupId>
<artifactId>equipmentdb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>equipmentdb</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.1.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.7</java.version>
</properties>

<dependencies>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
org.springframework.boot spring-boot-starter-jdbc
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-spring-boot-starter</artifactId>
        <version>1.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>


<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>runtime</scope>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-bom</artifactId>
            <version>7.5.5</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
[/code]Would be very nice if someone has got a hint for me.

Regards Chris

Hi Guys,

I am new to Vaadin and was going along well. I then started looking at Spring for Vaadin and went through a month of hell.

I then thought of ditching Vaadin because it ‘it does not play well with other frameworks’ and of course Spring is the standard.

I then went on to just looking at Spring with Maven and another month of hell. I then chatted to a guy at work who has been developing Java applications for 20 yrs and he said Java is always hectic on configuration and he ow just uses Java and JDBC and avoids ORM and Spring.

That said I then was about to go down this track and thought I would give Vaadin a last look because of time invested, thank goodness.

I am now back on track and loving Vaadin and have a great template together using Vaadin, Shiro for security and JDBC and am amazed at how much control you still have by mixing you own scss with the styles scss.

it is all easy again and I am not looking back so my advice is if you don’t have to use it to avoid Spring and ORM/JPA and just keep it simple. When things go wrong you don’t want to be looking at a generic stack trace from a framework that gives you no idea of what is wrong where. The small amount of time you save on typing with a framework like Spring will be cancelled out many times over figuring out what on earth is going wrong and the more complex your project gets the worse it will get, throw Maven depencies in the mix and then it gets really hairy. I think mix Vaadin (which I think is a complexity which is worth it) with spring (which I do not think is worth it) and complexity is now unmanageable, or at least for someone on my level.

My 2 cents

Cheers,

Hi,
so all the problems must have had sth. to do with my eclipse/tomcat setup. Starting all over with the Vaadin Spring tutorial setup (https://github.com/Vaadin/spring-tutorial), adding the Jdbc-template stuff and run it as Spring Boot App made everything working as it should.

Clad you made it work! Both Spring+Vaadin and Java EE (CDI) + Vaadin approaches work pretty well, I wonder what has been the issue with logan’s setup.

I have a spring jdbc template + vaadin turorial on my TODO. If you want to write one, it would be an awesome contribution for the community :wink:

cheers,
matti