繁体   English   中英

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

[英]I have problem with joining two entity classes

我在春季应用程序中编写了两个控制器类,称为玩家和团队,我想加入该模型类以连接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;
}

}

并以春季启动应用程序运行后,它给我以下错误

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

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

@SpringBootApplication 

或父包的任何子包。

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

更改package com.withAngular.team; package com.withAngular; 在团队课上。

更改package com.withAngular.demo.player; package com.withAngular; 在Player类中。

使用注释EnableJpaRepositories在你的类注释@SpringBootApplication并设置

basePackages属性basePackages一个通用包。

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

@EnableJpaRepositories(basePackages="com.withAngular")

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM