2

First off: Yes I have searched for previous questions. None of the answers work unfortunately.

I have created an entity class Product, a repository class ProductRepository and a main class Application.

Below you find the code:

Application

@SpringBootApplication
public class Application {

    public static void main (String[] args) {
        ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);

        ProductRepository repo = ctx.getBean(ProductRepository.class);
        Product product = new Product();
        product.setDescription("HDD");
        repo.save(product);
    }
}

Product

@Entity
@Table(name="Products")
public class Product {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int product_id;
    private String description;

    public int getProduct_id() {
        return product_id;
    }
    public void setProduct_id(int product_id) {
        this.product_id = product_id;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

ProductRepository

@Repository
public interface ProductRepository extends CrudRepository<Product, Integer> {

}

application.properties file

The file looks like this:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:8080/entmob
spring.datasource.username=entmob
spring.datasource.password=*******
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.show_sql=true
spring.jpa.hibernate.hbm2ddl.auto=create

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0   http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-relational-data-access</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>[5,]</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Compiling gives zero errors. However, the database does not get created in my localhost. I'm using XAMPP to manage my localhost.

Could somebody provide any help? Many thanks!

7
  • Did you tried official example of Spring Boot Data Jpa : spring.io/guides/gs/accessing-data-jpa Commented Oct 3, 2016 at 12:50
  • I would first try to remove the H2 dependency from the pom.xml and try . I suspect that your db is embedded. Commented Oct 3, 2016 at 14:17
  • 2
    When springboot finds the h2 dependency it will consider it as an webapp with embedded db and will ignore your mysql config. Commented Oct 3, 2016 at 14:18
  • 1
    You are completely right! fixed! Commented Oct 3, 2016 at 14:23
  • 1
    @Sergey Brunov . Done thank you Commented Oct 4, 2016 at 21:20

2 Answers 2

6

<property name="hibernate.hbm2ddl.auto">create</property> will create tables. But it will not create database. Change the connection url to generate the database as below.

jdbc:mysql://localhost:8080/entmob?createDatabaseIfNotExist=true

Use the below connector in pom.xml and above connection url.

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.38</version> <!-- or 5.1.28 / 5.1.30 -->
</dependency>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but I have already tried that. Does not work unfortunately!
EDIT: newDB has been created! I imported the SQL connector via maven and it works.
I would but I'm trying to do it with Hibernate and Spring via an application.properties file. If you're referring to the sql connector dependency, it is included in the pom.xml
that's fine, maybe the issue is with connector dependency. so try to add the connector jar directly in your classpath of your spring hibernate project instead of pom.xml. lets see it works or not?
Still no difference. I'll add my pom.xml to my question
1

Here is the solution. As i suggested in the comment section:

I would first try to remove the H2 dependency from the pom.xml and try . I suspect that your db is embedded.

When springboot finds the h2 dependency it will consider it as an webapp with embedded db and will ignore your mysql config

2 Comments

It is interesting. Could you please provide the appropriate reference to the documentation?
I cant remeber where this is referenced but Appently embedded configuration has precedence over non-embedded . Personnaly I thought that by manually specifying a second (non-embedded) db of our choice in the configuration (.properties file) would have overriden the default behaviour of SB but it doesnt apparently..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.