简体   繁体   中英

I have problem with joining two entity classes

I wrote two controller class in spring application called player and team and I want join this model classes for connect sql database and I write code but it give me error so please help me to resolve that I'm sure problem happen in below two files and my other dependencies and database connection working well

my Team class

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;
}

}

my Player class

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;
}

}

and after run as spring boot app it give me below error

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

It is a very simple problem, You need to put both the entity classes in same package and that package should be either the package which holds the main application class that is annotated with

@SpringBootApplication 

Or any of the sub package of parent package.

Eg: If package of your parent class is com.withAngular than put the Team and Player class also in the same package.

Change package com.withAngular.team; to package com.withAngular; in Team class.

Change package com.withAngular.demo.player; to package com.withAngular; in Player class.

Use the Annotation EnableJpaRepositories in your class annotated with @SpringBootApplication and set the

attribute basePackages to a common package.

So in your case com.withAngular .

@EnableJpaRepositories(basePackages="com.withAngular")

But it is better to moven entities in the same subpackage not in the "root" package of your application

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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