gpt4 book ai didi

java - 设计带有2个外键的spring data jpa注释类

转载 作者:行者123 更新时间:2023-12-01 18:42:20 27 4
gpt4 key购买 nike

因此,为了练习,我使用 Spring 构建一个 Java Web 应用程序。这是一个简单的彩票 API。

我决定在这个项目中使用 spring data JPA,而且我在基于 ORM 的数据库访问方面是个新手。
我还使用 Flyway 进行数据版本控制。因此,我使用 Flyway 脚本生成数据库,然后根据我的 spring data jpa 模型对其进行验证。

它相当复杂......但最终我认为它提供的不仅仅是它使事情变得复杂。

所以我有 3 张 table

RAFFLE
raffleID - primmary key
startDate
endDate
winningNumbers

TICKETS
ticketID - primmary key
raffleID - foreign key
customerName
numbers

WINNERS
ticketID - foreignKey
raffleID - foreignKey
Amount
Type

这样的设计很糟糕吗?我还应该使用 WINNERS 表的主键吗?可以创建 PFK 吗?我使用 MariaDB 作为数据库。

到目前为止我的 spring 代码:

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

目前我遇到了这个错误:

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

我知道我应该更多地了解 JPA 机制...但到目前为止,通过谷歌搜索,我还没有发现我面临的完全相同的问题。因此,我也将感谢您向我推荐有关此主题的任何资源!

谨致问候

编辑:发布此文后,我发现了这个问题: 2 Foreign Keys Into a New Table from Different Entities Hibernate建议使用关联表。有没有更简单的方法来做到这一点?

最佳答案

我决定暂时关闭flyway,只使用spring data jpa。我设法解决了所有问题。现在有 2 个外键的表可以工作了。我使用 @OneToOne 注释,因为一张票我们只能赢一次。但在抽奖中使用了@ManyToOne,因为抽奖中可能有多个获奖者。如果我错了,请纠正我。
我仍然希望能够使用 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;
}

关于java - 设计带有2个外键的spring data jpa注释类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59903148/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com