简体   繁体   English

设计带有 2 个外键的 Spring Data jpa 注释类

[英]design spring data jpa annotated class with 2 foreign keys

So for an exercise I am build an Java web app using Spring.因此,作为练习,我使用 Spring 构建了一个 Java Web 应用程序。 It is a simple lottery API.这是一个简单的彩票 API。

I decided to use spring data JPA in this project and I am quite a newbie in ORM based database access.我决定在这个项目中使用 spring 数据 JPA,我是基于 ORM 的数据库访问的新手。
I am also using flyway for data versioning.我也在使用 flyway 进行数据版本控制。 So I generate db using flyway scripts and then validate it against my spring data jpa models.因此,我使用 flyway 脚本生成 db,然后根据我的 spring 数据 jpa 模型对其进行验证。

It is quite complicated... but in the end I think it offers more than it complicates stuff.它非常复杂……但最终我认为它提供的不仅仅是使事情复杂化。

So I have 3 tables所以我有3张桌子

RAFFLE
raffleID - primmary key
startDate
endDate
winningNumbers

TICKETS
ticketID - primmary key
raffleID - foreign key
customerName
numbers

WINNERS
ticketID - foreignKey
raffleID - foreignKey
Amount
Type

Is this bad design?这是糟糕的设计吗? Should I also use primmary key for for WINNERS table?我还应该为 WINNERS 表使用主键吗? Would it be possible to create PFK ?是否可以创建 PFK ? I am using MariaDB as a database.我使用 MariaDB 作为数据库。

My spring code so far:到目前为止我的弹簧代码:

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

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long raffleID;

    private Timestamp startDate;
    private Timestamp endDate;
    private String winningNumbers;

    @OneToMany(mappedBy = "Tickets")
    private Set<Tickets> tickets;

    public Raffle(Timestamp startDate, Timestamp endDate, String winningNumbers,  Tickets... ticket){
        this.startDate = startDate;
        this.endDate = endDate;
        this.winningNumbers = winningNumbers;
    }
}

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

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long TicketID;

    @ManyToOne
    @JoinColumn(name="RAFFLE_ID")
    private Raffle RaffleID;

    private String customerName;
    private String numbers;

}

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

    @Id
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "raffleID", referencedColumnName = "raffleID")
    private Raffle raffle;

    @Id
    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "ticketID", referencedColumnName = "ticketID")
    private Tickets ticket;

    private Double prizeWon;
    private String prizeType;
    private String status;
}

Currently I am getting thrown this 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: mappedBy reference an unknown target entity property: com.grmkris.lightningloterry.model.database.Tickets.Tickets in com.grmkris.lightningloterry.model.database.Raffle.tickets

I know I should educate myself more on JPA mechanics... but so far with googling I haven't found exact same problem I am facing.我知道我应该对 JPA 机制进行更多的自我教育……但到目前为止,通过谷歌搜索我还没有发现我面临的完全相同的问题。 So I will also appreciate any resource you can recommend me on this topic!因此,我也将感谢您可以向我推荐有关此主题的任何资源!

Kinds regards亲切的问候

EDIT: just after posting this, I found this question: 2 Foreign Keys Into a New Table from Different Entities Hibernate It advises to use association table .编辑:在发布这个之后,我发现了这个问题: 2 Foreign Keys Into a New Table from Different Entities Hibernate它建议使用association table Is there any simpler way of doing this?有没有更简单的方法来做到这一点?

I decided to turn off flyway for now and just use spring data jpa.我决定暂时关闭 flyway,只使用 spring 数据 jpa。 I managed to fix all the problems.我设法解决了所有问题。 Now table with 2 foreign keys works.现在有 2 个外键的表可以工作了。 I use @OneToOne annotiation because 1 ticket can we winnable only once.我使用@OneToOne 注释,因为 1 张票我们只能赢一次。 But used @ManyToOne with Raffle because there can be multiple winners in the raffle.但是在抽奖中使用了@ManyToOne,因为抽奖中可以有多个获胜者。 Please correct me if I am wrong.如果我错了,请纠正我。
I'd still like to be able to use flyway but hibernate generates its own classes which are quite hard to replicate using sql by hand.我仍然希望能够使用 flyway,但 hibernate 会生成自己的类,这些类很难手动使用 sql 进行复制。

@Entity 
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Raffle{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long raffleID;

    private Timestamp startDate;
    private Timestamp endDate;
    private String winningNumbers;

    @OneToMany(mappedBy = "raffle")
    private Set<Tickets> tickets;

    @OneToMany(mappedBy="raffle")
    private Set<Winners> winner;
}

@Entity
@Data
@Builder
@Table(name = "Tickets")
public class Tickets {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long ticketID;

    @ManyToOne
    @JoinColumn(name="raffleID", nullable=false)
    private Raffle raffle;

    private String customerName;
    private String customerEmail;
    private String customerDescription;
    private Integer[] numbers;
    private String status;

    @OneToMany(mappedBy="ticket")
    private Set<Winners> winner;

}


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

    @EmbeddedId private WinnersID id;

    @ManyToOne @MapsId("raffleID")
    private Raffle raffle;

    @OneToOne @MapsId("ticketID")
    private Tickets ticket;

    private Double prizeWon;
    private String prizeType;
    private String status;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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