您的位置:首页 > 编程语言 > Java开发

spring boot 的 JPA 操作,提示Entity过时,No identifier specified for entity

2017-08-29 10:49 465 查看
先普及一下百度百科中对JPA的解释:JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。

Sun引入新的JPA ORM规范出于两个原因:其一,简化现有Java EE和Java SE应用开发工作;其二,Sun希望整合ORM技术,实现天下归一。

下面是spring boot基于MySql数据库的JPA操作。

pom.xml配置:

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

application.properties配置:
##datasource
spring.datasource.url = jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

## Java Persistence Api
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Hibernate ddl auto (create, create-drop, update)
spring.jpa.hibernate.ddl-auto = update
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# stripped before adding them to the entity manager)
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

这里在mysql连接字符串url中加入了ssl的配置:

jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true由于我的MySQL版本较高,如不加此配置,会报如下错误:
Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.


现在就可以使用Hibernate带来的好处了,在实体类注解@Entity就会自动进行表的DDL操作了。
package com.bocom.sb.entity;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Demo {
@Id
private int id;
private String name;
private int age;

public long 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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}这里是一个完整的entity类,目的是看上面import的包,在import包的时候,一定使用javax.persistence.*,否则将会出现错误,如下:
org.hibernate.AnnotationException: No identifier specified for entity这个错误看上去是这张表没有主键,在使用hibernate的映射表的时候entity类是必须要主键的,否则就会报出这个异常。但是我们已经配置了@ID,还会报这个错误是因为import错包了,错误的使用了org.springframework.data.annotation.Id包。

当我们使用@Entity的时候,提示我们Entity过时,被划了横线,是因错误的引用了org.hibernate.annotations.Entity包,这里应该使用JPA的注解javax.persistence.Entity,而不是hibernate的。

完成上述配置,运行程序,在数据库就会看到demo表了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息