我正在Spring Boot中配置Hibernate-出现以下问题。我已经添加了JAXB依赖项并检查了很多修复程序,但是对我来说不起作用。 请帮我解决。
配置
Java版本:Java 1.8
数据库-MS sql server2012
Spring Boot:2.1.0.RELEASE
错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method 'entityManagerFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/resource/beans/container/spi/BeanContainer
Application.java
package com.programmer.gate;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.programmer.gate.service.SoccerService;
@SpringBootApplication
public class Application implements CommandLineRunner{
@Autowired
SoccerService soccerService;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... arg0) throws Exception {
soccerService.addBarcelonaPlayer("Xavi Hernandez", "Midfielder", 6);
List<String> players = soccerService.getAllTeamPlayers(1);
for(String player : players)
{
System.out.println("Introducing Barca player => " + player);
}
}
}
Application.properties
server.port=9093
# create and drop tables and sequences, loads import.sql
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.show-sql=true
# Oracle settings
spring.datasource.url=jdbc:sqlserver://DESKTOP-WIN10:1433;databaseName=employee
spring.datasource.username=emp_db_connect
spring.datasource.password=abi@123
spring.datasource.driver.class=com.microsoft.sqlserver.jdbc.SQLServerDriver
# logging
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
logging.level.org.hibernate.SQL=debug
#logging.level.org.hibernate.type.descriptor.sql=trace
#logging.level.=debug
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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.programmer.gate</groupId>
<artifactId>SpringBootHibernate</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootHibernate</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>
<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>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.3.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>
JAVA代码
package com.programmer.gate.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
@Entity
public class Player {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "player_Sequence")
@SequenceGenerator(name = "player_Sequence", sequenceName = "PLAYER_SEQ")
private Long id;
@Column(name = "name")
private String name;
@Column(name = "num")
private int num;
@Column(name = "position")
private String position;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "team_id", nullable = false)
private Team team;
public Player() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public Team getTeam() {
return team;
}
public void setTeam(Team team) {
this.team = team;
}
}
package com.programmer.gate.model;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
@Entity
public class Team {
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "team_Sequence")
@SequenceGenerator(name = "team_Sequence", sequenceName = "TEAM_SEQ")
private Long id;
@Column(name = "name")
private String name;
@OneToMany(cascade = CascadeType.ALL,
fetch = FetchType.EAGER,
mappedBy = "team")
private List<Player> players;
public Team() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Player> getPlayers() {
return players;
}
}
package com.programmer.gate.repository;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.programmer.gate.model.Player;
@Repository
public interface PlayerRepository extends CrudRepository<Player, Long> {
List<Player> findByTeamId(long teamId);
}
package com.programmer.gate.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.programmer.gate.model.Team;
@Repository
public interface TeamRepository extends CrudRepository<Team, Long> {
Team findByPlayers(long playerId);
}
package com.programmer.gate.service;
import java.util.List;
public interface SoccerService {
public List<String> getAllTeamPlayers(long teamId);
public void addBarcelonaPlayer(String name, String position, int number);
}
package com.programmer.gate.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.programmer.gate.model.Player;
import com.programmer.gate.model.Team;
import com.programmer.gate.repository.PlayerRepository;
import com.programmer.gate.repository.TeamRepository;
@Service
public class SoccerServiceImpl implements SoccerService {
@Autowired
private PlayerRepository playerRepository;
@Autowired
private TeamRepository teamRepository;
public List<String> getAllTeamPlayers(long teamId) {
List<String> result = new ArrayList<String>();
List<Player> players = playerRepository.findByTeamId(teamId);
for (Player player : players) {
result.add(player.getName());
}
return result;
}
public void addBarcelonaPlayer(String name, String position, int number) {
//
Team barcelona = teamRepository.findOne(1l);
Player newPlayer = new Player();
newPlayer.setName(name);
newPlayer.setPosition(position);
newPlayer.setNum(number);
newPlayer.setTeam(barcelona);
playerRepository.save(newPlayer);
}
}