簡體   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