我是springboot,hibernate和jpa的完全新手。我早些时候发布了一个问题,但是以某种方式解决了这个问题,但是现在我陷入了另一个巨大的错误。
我试图从同一个数据库中获得两个单独的表。两个都有自己的模型和存储库类。当仅处理一个表但处理两个表时,该代码流畅运行:
2019-06-16 00:38:12.178 INFO 14868 --- [ main] org.hibernate.Version : HHH000412: Hibernate Core {5.0.12.Final}
2019-06-16 00:38:12.180 INFO 14868 --- [ main] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-06-16 00:38:12.181 INFO 14868 --- [ main] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2019-06-16 00:38:12.226 WARN 14868 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/metamodel/source/annotations/JPADotNames
Sun Jun 16 00:38:12 IST 2019 WARN: Caught while disconnecting... goes on and on....
applications.yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test_schema
username: root
password: user
driverClassName: com.mysql.jdbc.Driver
jpa:
generate-ddl: true
show-sql: true
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>com.techprimers.db</groupId>
<artifactId>spring-boot-mysqldb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-cleardb</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.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>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<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>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.8.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>5.0.1.Final</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
InterestDao.java-道类1
package com.techprimers.db.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.techprimers.db.model.Interest;
import com.techprimers.db.repository.InterestRepository;
@Service
public class InterestDao{
@Autowired
InterestRepository interrepo;
public Interest findOne(String token) {
return interrepo.findOne(token);
}
}
usersdao.java-道2类
package com.techprimers.db.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.techprimers.db.model.Users;
import com.techprimers.db.repository.UsersRepository;
@Service
public class userdao {
@Autowired
UsersRepository userrepo;
public Users findOne(String token) {
return userrepo.findOne(token);
}
}
Interest.java-模型类别1
package com.techprimers.db.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.springframework.stereotype.Repository;
@Repository
@Entity
@Table(name="hb_interest")
public class Interest{
@Column(name="ticket_no")
private String ticket_no;
@Column(name="hb_type")
private String hb_type;
@Column(name="hb_unit_hints")
private String hb_unit_hints;
@Column(name="hb_value")
private float hb_value;
public Interest() {
}
//getters and setters......
@Override
public String toString() {
return "{" +
"\"ticket_no\":" +"\"" + ticket_no + "\""+"," +
"\"Genre\":" +"\""+ hb_type + "\""+","+
"\"Powers\":"+"\"" + hb_unit_hints +"\""+
"\"Characters_No\":"+"\"" + hb_value +"\""+
'}';
}
}
Users.java模型类2
package com.techprimers.db.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.stereotype.Repository;
@Repository
@Entity
@Table(name="persons")
public class Users {
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
@Id
@Column(name = "token_id")
private String teamName;
@Column(name = "salary")
private Integer salary;
public Users() {
}
//getters and setters....
@Override
public String toString() {
return "{" +
"\"id\":" +"\"" + id + "\""+"," +
"\"value\":" +"\""+ name + "\""+","+
"\"Anime\":"+"\"" + teamName +"\""+
'}';
}
}
InterestRepository.java
package com.techprimers.db.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.techprimers.db.model.Interest;
public interface InterestRepository extends JpaRepository<Interest, String> {
}
UsersRepository.java
package com.techprimers.db.repository;
import com.techprimers.db.model.Users;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UsersRepository extends JpaRepository<Users, String> {
}
UsersResource.java控制器类
package com.techprimers.db.resource;
import com.techprimers.db.dao.InterestDao;
import com.techprimers.db.dao.userdao;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value = "/rest/users")
public class UsersResource {
@Autowired
userdao userDao;
InterestDao interdao;
@Autowired
ProcessData pd;
@GetMapping("/persons")
public JSONObject loadPersons(@RequestParam("access_token") String access_token) throws ParseException{
String decode_token = pd.testDecodeJWT(access_token); // Function to decode the access token
String token = pd.jsondata(decode_token);//Fetch the token from the decoded access token
String data= userDao.findOne(token).toString();//Convert the response data from table 1 to JSON String.
String med= interdao.findOne(token).toString();//Convert the response data from table 2 to JSON String
//Converting JSON String to JSON Objects
JSONObject json_data=pd.responseData(data);
JSONObject json_med=pd.responseData(med);
//Was only trying to return the 1st table data but the code stopped working.
return json_data;
}
}
我不知道错误的原因。我添加了外部休眠的jar容器,但这也没有用。 预先感谢。
答案 0 :(得分:3)
您不得指定与Spring Boot不同的Hibernate版本。
Spring Boot使用:5.0.12.Final
删除
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.8.Final</version>
</dependency>