如何通过JPA保留数据而无需主键 [英] How to persist data through JPA without needing a primary key

查看:1395
本文介绍了如何通过JPA保留数据而无需主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有流经我的应用程序的数据,通常不需要打扰,但要实现一项新功能,我需要临时存储它(例如1个小时).输入的数据可以与那里已经存在的数据完全相同,因此不需要主键.但是,使用JPA实体需要一个ID,但我不需要/想要一个.这使我无法正常工作.

I have data that flows through my application and normally it doesn't need to be bothered but to implement a new feature I need to store it temporarily (e.g. 1 hr). The data going in can be the exact same as something that is already in there so there is no need for a primary key. However, with JPA Entities need an Id but I don't need/want one. This is preventing me from getting it working.

这是通过Spring使用JPA实现的.由于数据经常移入和移出数据库,因此不建议使用自动生成的ID,因为它会在几年后通过ID.我试图使其可嵌入,并说它需要进行组件扫描以找到它的使用位置,但是如果我将其设置为实体,则它会提示我它需要主键.

This is through Spring using JPA. Since the data is moving in and out of the database frequently, the use of an auto generated ID is discouraged because it'll go through the IDs in a few years time. I have tried to make it embeddable to which it says I need to do a component scan to find where it is used but if I make it an entity then it gives me the error that it needs a primary key.

这是我的实体,用于存储我需要保留的数据.

This is my entity that stores the data I need to persist.

@Entity
@Table(name = "quoteOrderHistory")
public class QuoteOrderHistory {

    @Column(name = "storeNumber")
    private int storeNumber;

    @Column(name = "invoiceNumber")
    private String invoiceNumber;

    @Column(name = "quoteSaleDate")
    private Date quoteSaleDate;

    @Column(name="orderTotal")
    private BigDecimal orderTotal;

    @Column(name="orderHistoryDate")
    private Timestamp orderHistoryDate;

    // Constructors, Getters and Setters

}

这是我访问数据的存储库.

This is my repository to access the data.

@Repository
public interface QuoteOrderHistoryRepository extends JpaRepository<QuoteOrderHistory, Long> {

    @Query("DELETE FROM QuoteOrderHistory q WHERE q.orderHistoryDate > date")
    void deleteAllExpired(Date date);

    @Query("SELECT q FROM QuoteOrderHistory q WHERE q.storeNumber = ?1 AND q.invoiceNumber = ?2 ORDER BY q.orderHistoryDate DESC")
    List<QuoteOrderHistory> findAllByStoreAndInvoiceDesc(int storeNumber, String invoiceNumber);
}

我不知道该怎么做.同样,不需要主键,因为它假定支持重复的条目.如果有另一种方法可以不使用JPA,那么我全力以赴,但是目前看来,持久保存数据是最容易的.如果您需要更多信息,请告诉我.我也可能缺少可以避免这些事情的方法,但是我对JPA并不熟悉.因此,感谢所有帮助.

I can't figure out to get this to work. Again a primary key isn't needed since it's suppose to support duplicate entries. If there is another way around this without using JPA then I'm all for it but currently it seems to be the easiest to persist the data. If you need anymore information just let me know. I also might be missing something that can be done to avoid this all together but I'm not that familiar with JPA. So all help is appreciated.

推荐答案

Jazzepi说的是正确的,但严格要求我不要使用自动生成的数字作为ID.因此,人们在此处进行了链接,以使用UUID进行描述.这是解决此问题的最佳选择,因为数据库中的对象被安排在其中的时间不超过几个小时.既然是这种情况,那么UUID永远不会溢出,并且在任何给定时间在表内重复UUID的可能性几乎为零,因为大多数人不会留在那儿.

Jazzepi stated was correct but I was strictly requested not to use an auto generated number as the ID. Therefore, people linked this here depicting using a UUID. This is the best choice for this problem since the objects in the database are timed to be in there no more than a few hours. Since this is the case, a UUID will never overflow and the likelihood of a repeated UUID inside of the table any given time is almost zero since most won't stay there.

新实体类:

@Entity
@Table(name = "quoteOrderHistory")
public class QuoteOrderHistory {

    @Id
    @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
    @Column(name = "uuid", unique = true)
    private String uuid;

    @Column(name = "storeNumber")
    private int storeNumber;

    @Column(name = "invoiceNumber")
    private String invoiceNumber;

    @Column(name = "quoteSaleDate")
    private Date quoteSaleDate;

    @Column(name="orderTotal")
    private BigDecimal orderTotal;

    @Column(name="orderHistoryDate")
    private Timestamp orderHistoryDate;

    // Constructor, getters, setters

}

这篇关于如何通过JPA保留数据而无需主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆