【问题标题】:I have problem with joining two entity classes我在加入两个实体类时遇到问题
【发布时间】:2019-08-23 05:23:43
【问题描述】:

我在 spring 应用程序中编写了两个控制器类,名为 player 和 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

【问题讨论】:

标签: sql spring spring-boot spring-mvc


【解决方案1】:

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

@SpringBootApplication 

或父包的任何子包。

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

在 Team 类中将 package com.withAngular.team; 更改为 package com.withAngular;

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

【讨论】:

    【解决方案2】:

    在您的班级中使用注释EnableJpaRepositories 并使用@SpringBootApplication 进行注释并设置

    basePackages 归属于一个通用包。

    所以在你的情况下com.withAngular

    @EnableJpaRepositories(basePackages="com.withAngular")
    

    但最好在同一个子包中移动实体,而不是在应用程序的“根”包中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多