Can't create table with jpa+spring boot
15,955
Solution 1
Try to add @EntityScan
@EntityScan("<package with entities>")
@SpringBootApplication
public class Application { ... }
Solution 2
With the current information I have a couple of suggestions / Questions that might help you allong:
- Does the "springapp" database exist with the correct user (root) and password (me) assigned to it in MySQL
- Has MySQL been started?
I see no @Repository definition in your code e.g.
@Repository public interface CustomerRepository extends JpaRepository{}
Related videos on Youtube
Author by
Daniel
Updated on June 04, 2022Comments
-
Daniel almost 2 years
I'm trying to generate table using JPA. but i can't create it. There is no error in the code, but it seems that there is configuration error. but i can't find it, i tried many configurations but nothing happen. Thanks a lot.
This is
application.property
:spring.datasource.url=jdbc:mysql://localhost:3306/springapp spring.datasource.username= root spring.datasource.password= me spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.hibernate.ddl-auto=create
this is my class:
@Entity public class Customer { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; private String name; private String phone; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public Customer(String name, String phone) { super(); this.name = name; this.phone = phone; } public Customer() { super(); } }
This is the application:
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
And this is the controller:
@RestController @RequestMapping("/customer") public class CustomerController { @Autowired CustomerRepository customerRepository;
@RequestMapping("/findall") @ResponseBody public List<Customer> findAll() { return customerRepository.findAll(); }
}
Pom.xml
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>GStock-3</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>GStock-3</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
Amol Raje over 6 yearsit seems right ..what error you get? is table created in mysql?
-
Daniel over 6 yearswhen i check the database, i can't find the table castomer
-
Amol Raje over 6 yearsgo through it:spring.io/guides/gs/accessing-data-mysql
-
Lijo about 4 yearshi do you have sample code github
-
-
Daniel over 6 yearsyes i have interface extends jpaRepository. the springapp exists with the same username and password. i did: use springapp; show tables:empty set
-
Daniel over 6 yearsThe same thing, it's really wreid, !
-
Daniel over 6 yearsNote: I was working with normal spring, with the same username and password, and i used jpa anad i generated tables normally. But now, i moved to spring boot it don't work, i think it's configuration problem.
-
Daniel over 6 yearswhere in customer.java?
-
Nikolai Shevchenko over 6 yearsNo. Near @SpringBootApplication public class Application
-
Daniel over 6 yearsYesss it works finally, I added @EntityScan("<package with entities>") in application.java. Thank you nikolay and all the others for you help.
-
pixel over 2 yearsThanks @NikolaiShevchenko but with or witout
@EntityScan
, I get error "com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-551, SQLSTATE=42501, SQLERRMC=MY_USER;CREATE TABLE;DSNDB04, DRIVER=4.29.24" I use Spring Data JPA and by data source is DB2 database. Any idea why I am getting this error? It seem that user MY_USER does not have priviledge to issue CREATE TABLE command; however, I dont know what are priviledges required by Hibernate / JPA. -
Nikolai Shevchenko over 2 years@pixel user on whose behalf SQL commands are run might require some set of privileges, but JPA (including Hibernate engine) itself doesn't require any privilieges.
-
pixel over 2 yearsThanks @NikolaiShevchenko. I figured out what the problem was. For DB2, I need to provide schema to my entity's @Table annotation like
@Entity @Immutable @Table(name = "CAR_VIEW", schema = "MY_SCHEMA") public class CarEntity implements Serializable { ...
. With that, the problem with DB2 is gone but now my H2 stopped working so need to figure out how to use entity with schema with H2 database?