我在加入两个实体类时遇到问题

时间:2019-08-23 05:23:43

标签: sql spring spring-boot spring-mvc

我在spring应用程序中编写了两个控制器类,称为player and team,我想加入该模型类以连接sql数据库,并且编写了代码,但是它给了我错误,所以请帮助我解决以下问题两个文件以及其他依赖项和数据库连接都很好

我的团队班级

package com.withAngular.team;

import java.util.ArrayList;
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.Table;

import org.springframework.beans.factory.annotation.Autowired;

import com.withAngular.demo.player.Player;

@Entity
@Table(name = "team")
public class Team {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "team")
private String team;
@Column(name = "description")
private String description;
@Column(name = "owner")
private String owner;
@Column(name = "total_played")
private int totalPlayed;
@Column(name = "total_won")
private int totalWon;
@Column(name = "total_lost")
private int totalLost;
@Column(name = "no_result")
private int noResult;

@OneToMany
(mappedBy = "team", cascade = CascadeType.MERGE, fetch = FetchType.LAZY)
private List<Player> players = new ArrayList<>();

public Team(int id, String name, String description, String owner, int totalplayed, int totalwon, int totallost, int noresult) {
    this.setId(id);
    this.setDescription(description);
    this.setOwner(owner);
    this.setTotalPlayed(totalplayed);
    this.setTotalWon(totalwon);
    this.setTotalLost(totallost);
    this.setNoResult(noresult);
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getTeam() {
    return team;
}

public void setTeam(String team) {
    this.team = team;
}

public List<Player> getPlayers() {
    return players;
}

public void setPlayers(List<Player> players) {
    this.players = players;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getOwner() {
    return owner;
}

public void setOwner(String owner) {
    this.owner = owner;
}

public int getTotalPlayed() {
    return totalPlayed;
}

public void setTotalPlayed(int totalPlayed) {
    this.totalPlayed = totalPlayed;
}

public int getTotalWon() {
    return totalWon;
}

public void setTotalWon(int totalWon) {
    this.totalWon = totalWon;
}

public int getTotalLost() {
    return totalLost;
}

public void setTotalLost(int totalLost) {
    this.totalLost = totalLost;
}

public int getNoResult() {
    return noResult;
}

public void setNoResult(int noResult) {
    this.noResult = noResult;
}

}

我的播放器类

package com.withAngular.demo.player;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.withAngular.team.Team;

@Entity
// @Table(name=PLAYER) when table name different from the class name
public class Player {

@Id // primary key
@GeneratedValue(strategy = GenerationType.AUTO) // auto increment
private int id;
// @Column(name = "PlayerName") when db table column name different from the
// property name assigned below
private String playerName;
private String preference;
@Column(name= "match_played")
private int matchPlayed;
private int runs;
private int wickets;
@Column(name= "highest_score")
private int highestScore;
@Column(name="best_wicket")
private String bestWicket;
private int fifties;
private int centuries;
private int thirties;
private int catches;
private int stumpings;
private int fours;
private int sixes;
@Column(name = "strike_rate")
private double strikeRate;
private double average;

@ManyToOne(targetEntity = Team.class)
@JoinColumn(name= "team_id")
private Team team;

// getters and setters

public Player(int id, String playername, String preference, int matchplayed, int runs, int wickets, int highestscore, String bestWicket, int fifties, int centuries, int thirties, int caches, int stumpings,int fours, int sixes, double strikerate, double average) {
    // TODO Auto-generated constructor stub
    this.setId(id);
    this.setPlayerName(playername);
    this.setPreference(preference);
    this.setMatchPlayed(matchplayed);
    this.setRuns(runs);
    this.setWickets(wickets);
    this.setHighestScore(highestscore);
    this.setBestWicket(bestWicket);
    this.setFifties(fifties);
    this.setCenturies(centuries);
    this.setThirties(thirties);
    this.setCatches(caches);
    this.setStumpings(stumpings);
    this.setFours(fours);
    this.setSixes(sixes);
    this.setStrikeRate(strikerate);
    this.setAverage(average);
}
public int getId() {
    return id;
}
public String getPreference() {
    return preference;
}
public void setPreference(String preference) {
    this.preference = preference;
}
public int getMatchPlayed() {
    return matchPlayed;
}
public void setMatchPlayed(int matchPlayed) {
    this.matchPlayed = matchPlayed;
}
public int getRuns() {
    return runs;
}
public void setRuns(int runs) {
    this.runs = runs;
}
public int getWickets() {
    return wickets;
}
public void setWickets(int wickets) {
    this.wickets = wickets;
}
public int getHighestScore() {
    return highestScore;
}
public void setHighestScore(int highestScore) {
    this.highestScore = highestScore;
}
public String getBestWicket() {
    return bestWicket;
}
public void setBestWicket(String bestWicket) {
    this.bestWicket = bestWicket;
}
public int getFifties() {
    return fifties;
}
public void setFifties(int fifties) {
    this.fifties = fifties;
}
public int getCenturies() {
    return centuries;
}
public void setCenturies(int centuries) {
    this.centuries = centuries;
}
public int getThirties() {
    return thirties;
}
public void setThirties(int thirties) {
    this.thirties = thirties;
}
public int getCatches() {
    return catches;
}
public void setCatches(int catches) {
    this.catches = catches;
}
public int getStumpings() {
    return stumpings;
}
public void setStumpings(int stumpings) {
    this.stumpings = stumpings;
}
public int getFours() {
    return fours;
}
public void setFours(int fours) {
    this.fours = fours;
}
public int getSixes() {
    return sixes;
}
public void setSixes(int sixes) {
    this.sixes = sixes;
}
public double getStrikeRate() {
    return strikeRate;
}
public void setStrikeRate(double strikeRate) {
    this.strikeRate = strikeRate;
}
public double getAverage() {
    return average;
}
public void setAverage(double average) {
    this.average = average;
}
public Team getTeam() {
    return team;
}
public void setTeam(Team team) {
    this.team = team;
}
public void setId(int id) {
    this.id = id;
}
public String getPlayerName() {
    return playerName;
}
public void setPlayerName(String playerName) {
    this.playerName = playerName;
}

}

作为Spring Boot应用程序运行后,它给我以下错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.withAngular.demo.player.Player.team references an unknown entity: com.withAngular.team.Team

2 个答案:

答案 0 :(得分:1)

这是一个非常简单的问题,您需要将两个实体类放在同一程序包中,并且该程序包应该是包含以

注释的主应用程序类的程序包。
@SpringBootApplication 

或父包的任何子包。

例如:如果您的父类的软件包是com.withAngular,则将Team和Player类也放在同一软件包中。

在小组课程中将package com.withAngular.team;更改为package com.withAngular;

在播放器类中将package com.withAngular.demo.player;更改为package com.withAngular;

答案 1 :(得分:0)

在课堂上使用带有@SpringBootApplication注释的注释EnableJpaRepositories并设置

basePackages归于一个通用软件包。

因此,在您的情况下,com.withAngular

@EnableJpaRepositories(basePackages="com.withAngular")

但是最好将实体移至同一子包中,而不要移至应用程序的“根”包中