我无法弄清楚为什么JPA无法生成匹配表

时间:2018-09-24 22:27:58

标签: java spring-boot jpa

我不知道为什么该类不适用于JPA。必须与我选择的注释有关。 我正在尝试创建一个足球比赛,该比赛在某个特定日期与特定角色的用户进行约会。

@Entity
public class Match {
    @Id
    @GeneratedValue
    private Integer id;

    @OneToMany
    @Column(name = "user")
    private List<User> players;

    @OneToMany
    @Column(name = "user")
    private List<User> administrators;

    @OneToOne
    private User owner;

    @OneToOne
    private Field field;

    private Calendar calendar;

    public Match(User owner) {
        this.owner = owner;
        this.administrators = new ArrayList<>(Arrays.asList(owner));
        this.players = new ArrayList<>(Arrays.asList(owner));
    }

   // getter and setters
}

2 个答案:

答案 0 :(得分:0)

首先删除它们在@OneToMany上无用的@Column注释。

然后,您没有默认的构造函数。 JPA要求构造函数对象使用它。

所以您的班级应该像这样:

@Entity
public class Match {
    @Id
    @GeneratedValue
    private Integer id;

    @OneToMany
    private List<User> players;

    @OneToMany
    private List<User> administrators;

    @OneToOne
    private User owner;

    @OneToOne
    private Field field;

    private Calendar calendar;

    public Match() {
    }

    public Match(User owner) {
        this.owner = owner;
        this.administrators = new ArrayList<>(Arrays.asList(owner));
        this.players = new ArrayList<>(Arrays.asList(owner));
    }

    // getter and setters
}

答案 1 :(得分:0)

实体类.....

@Entity
public class Match {

private Integer id;
private List<User> players;
private List<User> administrators;
private User owner;
private Field field;
private Calendar calendar;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
    return id;
}

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

@OneToMany(mappedBy = "<variable name in user Class>", targetEntity = User.class)
public List<User> getPlayers() {
    return players;
}

public void setPlayers(List<User> players) {
    this.players = players;
}

@OneToMany(mappedBy = "<variable name in user Class>", targetEntity = User.class)
public List<User> getAdministrators() {
    return administrators;
}

public void setAdministrators(List<User> administrators) {
    this.administrators = administrators;
}

@OneToOne
@JoinColumn(name = "userId")
public User getOwner() {
    return owner;
}

public void setOwner(User owner) {
    this.owner = owner;
}

@OneToOne
@JoinColumn(name = "fieldId")
public Field getField() {
    return field;
}

public void setField(Field field) {
    this.field = field;
}

@Column(name ="calender")
public Calendar getCalendar() {
    return calendar;
}

public void setCalendar(Calendar calendar) {
    this.calendar = calendar;
}

}