6

I am doing a spring-boot project and trying to create a table with hibernate, I get no errors when I run the app and the server starts normally, but the table does not get created.

StatusUpdate.java

package model; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.PrePersist; @Entity @Table(name="status_update") public class StatusUpdate { @Id @Column(name="id") @GeneratedValue(strategy=GenerationType.AUTO) private Long id; @Column(name="text") private String text; @Column(name="added") @Temporal(TemporalType.TIMESTAMP) private Date added; @PrePersist protected void onCreate() { if (added == null) { added = new Date(); } } public StatusUpdate(String text) { this.text = text; } public StatusUpdate(String text, Date added) { this.text = text; this.added = added; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public Date getAdded() { return added; } public void setAdded(Date added) { this.added = added; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((added == null) ? 0 : added.hashCode()); result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + ((text == null) ? 0 : text.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; StatusUpdate other = (StatusUpdate) obj; if (added == null) { if (other.added != null) return false; } else if (!added.equals(other.added)) return false; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; if (text == null) { if (other.text != null) return false; } else if (!text.equals(other.text)) return false; return true; } } 

pom.xml

<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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> <tiles.version>3.0.7</tiles.version> </properties> <groupId>com.voja</groupId> <artifactId>spring-boot-tutorial</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-core</artifactId> <version>${tiles.version}</version> </dependency> <dependency> <groupId>org.apache.tiles</groupId> <artifactId>tiles-jsp</artifactId> <version>${tiles.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>1.3.5.RELEASE</version> </dependency> </dependencies> <packaging>war</packaging> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> </plugins> </build> </project> 

application.properties

debug=true spring.datasource.url=jdbc:mysql://localhost:3306/springboottutorial spring.datasource.username=springboot spring.datasource.password=hello spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect spring.jpa.generate-ddl=true spring.jpa.show-sql=true logging.level.org.hibernate.SQL=DEBUG 

I also get a yellow line under dialect in this line spring.jpa.hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialectwhich says `spring.jpa.hibernate.dialect' is an unknown property. Did you mean 'spring.jpa.hibernate.ddl-auto' in case that might be a problem.

5
  • whats does your persistence.xml look like? Commented Sep 9, 2016 at 11:32
  • As @911DidBush mentioned please show your persistence.xml. The parameter hbm2ddl.auto is important here. Commented Sep 9, 2016 at 11:36
  • you can add spring.jpa.show-sql=true to your application.properties to see more logs. Maybe you can find something Commented Sep 9, 2016 at 11:37
  • @911DidBush I am new to java and spring. Where is that file? I did not create one if I was supposed to, I am following a tutorial and that wasn't done. Commented Sep 9, 2016 at 11:48
  • I also added my application,properties file to the Q. Commented Sep 9, 2016 at 11:51

6 Answers 6

4

Sometimes the problem occurs in the property name you choose. Like text, value etc. which are reserved keyword for database. Hence you can see a log of creating table but table will not be created. To resolve this just change you property name in you bean or provide explicit column name.

Example: property or column name "value" is not supported in mysql. Hence my tables were not created. I have to rename it to 'valueString'.

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

I guess this was supposed to be a comment and you didnt have enough reputation?
3

Found the problem by reading another post, actually there was a problem with packages and classes within them, they couldn't find one another and so the table wasn't created.

I made a new project and put all classes inside the same package and it worked, so I will fix my existing project based on that.

Comments

1

It's not working for you, because you are not using it, I mean, JPA creates the database when needs to use it. You can try to write a test that uses it, or add a rest repository to try it, just add this to your application.properties;

 spring.jpa.hibernate.ddl-auto=create 

and then create this interface:

 @RepositoryRestResource public interface IStatusRepository extends CrudRepository<StatusUpdate, Long> { } 

You will need also this dependency;

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> 

It's more easy just creating a test, but I guess you are trying to build a rest web service, so try this to see it works =)

3 Comments

I am not trying to create a rest web service, just a basic website (following a tutorial). Is there something I could add to StausUpdate.jjava in order to make it work?
I created your same project, with this application.properties and it just work fine, table is created without any problem with your same code: spring.datasource.url=jdbc:mysql://localhost:3306/suscripciones?useSSL=false spring.datasource.name = dataSource spring.datasource.username=root spring.datasource.password=root spring.jpa.hibernate.ddl-auto=create
is it in eclipse? If so, could you zip the whole project and upload it somewhere so I can compare?
1

For your application.properties file follow below link for this

it is like : spring.jpa.generate-ddl=true and then add spring.jpa.hibernate.ddl-auto=create-drop

http://docs.spring.io/spring-boot/docs/1.2.3.RELEASE/reference/htmlsingle/#common-application-properties

1 Comment

spring.jpa.generate-ddl=true and then add spring.jpa.hibernate.ddl-auto=create-drop
-1

If you are using jpa and hibernate then add the following property in your application.properties file

spring.jpa.hibernate.ddl-auto=update 

2 Comments

change the dialect to this : org.hibernate.dialect.MySQLDialect
try adding all the properties listed in this link
-1

Try adding this to your application.properties :

spring.jpa.hibernate.ddl-auto=create 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.