Hibernate中的一对多关联表

时间:2019-06-06 15:14:36

标签: java database oracle hibernate

我试图学习Hibernate,并从以下问题开始,我正在为运动成绩应用程序建模。我有以下Oracle表。

我有一个LEAGUES表,其主键为LEAGUE_ID,一个TEAMS表,其主键为TEAM_ID,外键为LEAGUE_ID。一个联赛将包含许多球队。

LEAGUES表描述为...。

BaseFilterPage

还有TEAMS。...

Name            Null     Type         
--------------- -------- ------------ 
LEAGUE_ID       NOT NULL NUMBER(38)   
LEAGUE_NAME     NOT NULL VARCHAR2(30) 
NUMBER_OF_TEAMS          NUMBER(38)   
GOALS                    VARCHAR2(1)  
CORNERS                  VARCHAR2(1)  
START_DATE               DATE         

我正在尝试遵循本教程: https://www.baeldung.com/hibernate-one-to-many

我的Entity类如下,@Data是lombok批注,因此将自动生成getter / setters,构造函数...

Name               Null     Type          
--------------- -------- ------------ 
TEAM_ID            NOT NULL NUMBER(38)   
LEAGUE_ID          NOT NULL NUMBER(38)   
TEAM_NAME          NOT NULL VARCHAR2(40) 
PREVIOUS_LEAGUE_ID NOT NULL NUMBER(38)   
STADIUM_NAME                VARCHAR2(40) 
CURRENT_GRADE               VARCHAR2(1)  
CITY                        VARCHAR2(30) 
@Data
@Entity
@Table(name="Leagues")
public class League {

    @Id
    @Column(name="league_id", updatable=false, nullable=false)
    private int leagueId;

    @Column(name="league_name")
    private String leagueName;

    @Column(name="number_of_teams")
    private int numberOfTeams;

    private String goals;

    private String corners;

    @Column(name="start_date")
    private Date startDate;

    @OneToOne(mappedBy = "league")
    private Set<Team> teams;
}

我只是想立即使用此内容检索数据...

@Data
@Entity
@Table(name="Teams")
public class Team {

    @Id
    @Column(name="team_id", updatable=false, nullable=false)
    private int teamId;

    @ManyToOne
    @JoinColumn(name="league_id")
    private League league;

    @Column(name="team_name")
    private String teamName;

    @Column(name="previous_league_id")
    private int previousLeagueId;

    @Column(name="stadium_name")
    private String stadiumName;

    @Column(name="current_grade")
    private String currentGrade;

    @Column(name="city")
    private String city;
}

但是我得到以下异常;

public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

    @Transactional(readOnly = true)
    @Override
    public void run(String... args) throws Exception {

        System.out.println("DATASOURCE = " + dataSource);

        System.out.println("\n1.findAll()...");
        for (Team team : teamRepository.findAll()) {
            System.out.println(team);
        }

        for (League league : leagueRepository.findAll()) {
            System.out.println(league);
        }
    ...
    }

在尝试引入映射之前,我之前已经工作过,所以主类和龙目岛应该没问题。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

在这里

name

我想你是说

dummyVar