Hibernate使用@OneToMany或@ManyToMany定位未映射的类

时间:2018-01-22 15:28:05

标签: java hibernate

我已经阅读了很多主题但仍然无法解决我的问题。 问题:标题出错。我每次尝试从数据库中读取实体时都会得到它。

数据库是mysql,由jdbc驱动程序连接。

这是我的代码:第一个是ReadStudentDemo(从数据库中读取)

package entities;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.EntityTransaction;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;



public class ReadStudentDemo {

    public static void main(String[] args) throws ParseException {

        Player roggy = new Player();


        Configuration conf = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Player.class);

        ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();

        SessionFactory sf = conf.buildSessionFactory(sr);

        Session session = sf.openSession();

        EntityTransaction tx = session.beginTransaction();

        roggy = (Player)session.get(Player.class, 4);

        tx.commit();

        System.out.println(roggy);

    }

}

第二个是播放器实体映射的Player.java。

package entities;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Player generated by hbm2java
 */
@Entity
@Table(name = "Player", catalog = "sql11217012")
public class Player implements java.io.Serializable {

    private Integer id;
    private String firstName;
    private String lastName;
    private Date birth;
    private Float wl;
    private Integer win;
    private BigDecimal money;
    private Set<Match> matchesForPlayerId = new HashSet<Match>(0);
    private Set<Match> matchesForPlayer1Id = new HashSet<Match>(0);
    private Set<Match> matchesForCourtId = new HashSet<Match>(0);

    public Player() {
    }

    public Player(String firstName, String lastName, Date birth, Float wl, Integer win, BigDecimal money,
            Set<Match> matchesForPlayerId, Set<Match> matchesForPlayer1Id, Set<Match> matchesForCourtId) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.birth = birth;
        this.wl = wl;
        this.win = win;
        this.money = money;
        this.matchesForPlayerId = matchesForPlayerId;
        this.matchesForPlayer1Id = matchesForPlayer1Id;
        this.matchesForCourtId = matchesForCourtId;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)

    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

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

    @Column(name = "first_name", length = 45)
    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name = "last_name", length = 45)
    public String getLastName() {
        return this.lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Temporal(TemporalType.DATE)
    @Column(name = "birth", length = 10)
    public Date getBirth() {
        return this.birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    @Column(name = "wl", precision = 12, scale = 0)
    public Float getWl() {
        return this.wl;
    }

    public void setWl(Float wl) {
        this.wl = wl;
    }

    @Column(name = "win")
    public Integer getWin() {
        return this.win;
    }

    public void setWin(Integer win) {
        this.win = win;
    }

    @Column(name = "money", precision = 15)
    public BigDecimal getMoney() {
        return this.money;
    }

    public void setMoney(BigDecimal money) {
        this.money = money;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "playerByPlayerId")
    public Set<Match> getMatchesForPlayerId() {
        return this.matchesForPlayerId;
    }

    public void setMatchesForPlayerId(Set<Match> matchesForPlayerId) {
        this.matchesForPlayerId = matchesForPlayerId;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "playerByPlayer1Id")
    public Set<Match> getMatchesForPlayer1Id() {
        return this.matchesForPlayer1Id;
    }

    public void setMatchesForPlayer1Id(Set<Match> matchesForPlayer1Id) {
        this.matchesForPlayer1Id = matchesForPlayer1Id;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "playerByCourtId")
    public Set<Match> getMatchesForCourtId() {
        return this.matchesForCourtId;
    }

    public void setMatchesForCourtId(Set<Match> matchesForCourtId) {
        this.matchesForCourtId = matchesForCourtId;
    }

}

然后你有Match实体。

package entities;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "Match", catalog = "sql11217012")
public class Match implements java.io.Serializable {

    private Integer id;
    private Player playerByPlayerId;
    private Player playerByPlayer1Id;
    private Player playerByCourtId;

    public Match() {
    }

    public Match(Player playerByPlayerId, Player playerByPlayer1Id, Player playerByCourtId) {
        this.playerByPlayerId = playerByPlayerId;
        this.playerByPlayer1Id = playerByPlayer1Id;
        this.playerByCourtId = playerByCourtId;
    }

    @Id
    @GeneratedValue(strategy = IDENTITY)

    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "player_id", nullable = false)
    public Player getPlayerByPlayerId() {
        return this.playerByPlayerId;
    }

    public void setPlayerByPlayerId(Player playerByPlayerId) {
        this.playerByPlayerId = playerByPlayerId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "player1_id", nullable = false)
    public Player getPlayerByPlayer1Id() {
        return this.playerByPlayer1Id;
    }

    public void setPlayerByPlayer1Id(Player playerByPlayer1Id) {
        this.playerByPlayer1Id = playerByPlayer1Id;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "court_id", nullable = false)
    public Player getPlayerByCourtId() {
        return this.playerByCourtId;
    }

    public void setPlayerByCourtId(Player playerByCourtId) {
        this.playerByCourtId = playerByCourtId;
    }

}

然后是堆栈跟踪

    INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Exception in thread "main" org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: entities.Player.matchesForCourtId[entities.Match]
    at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1253)
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:810)
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:735)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:54)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1589)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
    at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:691)
    at entities.ReadStudentDemo.main(ReadStudentDemo.java:50)

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您将Player添加到配置

Configuration conf = new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Player.class);

但似乎你从未添加Match.class

所以你应该使用

Configuration conf = new Configuration()
    .configure("hibernate.cfg.xml")
    .addAnnotatedClass(Player.class)
    .addAnnotatedClass(Match.class);