简体   繁体   English

H2 嵌入式数据库外键和引用

[英]H2 Embedded Database foreign keys and references

So, I have a homework where I need to implement two entities - Account and Transaction Futher on I need to be able to create a transaction between two accounts (scenario: only 2 account involved)所以,我有一个作业,我需要实现两个实体 - 账户和交易进一步我需要能够在两个账户之间创建交易(场景:只涉及 2 个账户)

I also need to find out table's structure for Transaction and would appreciate a help for Transaction table creation script.我还需要找出交易表的结构,并希望得到交易表创建脚本的帮助。

My question is: I need to find out how to reference the foreign keys and relationship between these two tables as for now it is a bit unclear for me.我的问题是:我需要找出如何引用这两个表之间的外键和关系,因为现在对我来说有点不清楚。

Logic is next: I was thinking and maybe an Account would have two List<Transaction> as in:接下来是逻辑:我在想,也许一个Account会有两个List<Transaction>如下所示:

private List<Transaction> transactionsMade; 
private List<Transaction> transactionsReceived.

therefore: in Transaction class would be:因此:在Transaction类中将是:

@ManyToOne referencing the transactionMadeList @ManyToOne引用transactionMadeList

private Account emitter; 

@ManyToOne referencing the transactionReceivedList @ManyToOne引用transactionReceivedList

private Account receptor;

So here are my classes所以这是我的课程

Account.class账户类

public class Account {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    private String holder;

    @NotNull
    private Integer balance;

    @OneToMany(mappedBy = "emitter",fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    private List<Transaction> transactionsMade;

    @OneToMany(mappedBy = "receptor",fetch = FetchType.EAGER,cascade = CascadeType.ALL)
    private List<Transaction> transactionsReceived;

}

Transaction.class事务类

public class Transaction {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    private Integer amount;

    @NotNull
    private LocalDateTime created;

    @ManyToOne
    @JoinColumn(name = "transactionEmitter")
    private Account emitter;

    @ManyToOne
    @JoinColumn(name = "transactionReceptor")
    private Account receptor;

    public Transaction(Integer amount, Account emitter, Account receptor){
        this.created = LocalDateTime.now();
        this.amount = amount;
        this.emitter = emitter;
        this.receptor = receptor;
    }

}

For now that's it.现在就是这样。

Thank You in advance!先感谢您! :) :)

You are thinking in the right direction.您正在朝着正确的方向思考。 Here is how your schema will look like:以下是您的架构的外观: 数据库模式

The only two things I noticed you could do better:我注意到你可以做得更好的仅有两件事:

1) Use Instant instead of LocalDateTime , so it will be stored in UTC time. 1) 使用Instant而不是LocalDateTime ,因此它将以UTC时间存储。

Change this:改变这个:

private LocalDateTime created;

To this:对此:

private Instant created;

2) Do not use EAGER initialization: 2)不要使用EAGER初始化:

@OneToMany(mappedBy = "emitter",fetch = FetchType.EAGER,cascade = CascadeType.ALL)
private List<Transaction> transactionsMade;

@OneToMany(mappedBy = "receptor",fetch = FetchType.EAGER,cascade = CascadeType.ALL)
private List<Transaction> transactionsReceived;

will become this:会变成这样:

@OneToMany(mappedBy = "emitter",fetch = FetchType.LAZY,cascade = CascadeType.ALL)
private List<Transaction> transactionsMade;

@OneToMany(mappedBy = "receptor",fetch = FetchType.LAZY,cascade = CascadeType.ALL)
private List<Transaction> transactionsReceived;

If you get famous LazyInitializationException , try wrapping it around @Transactional .如果您遇到著名的LazyInitializationException ,请尝试将其包裹在@Transactional周围。 It might complicate it a bit, however, in the long run, will save you some server resources, because you won't be loading all account's transaction when they are not even needed.这可能会使它有点复杂,但是,从长远来看,会为您节省一些服务器资源,因为当您甚至不需要它们时,您将不会加载所有帐户的交易。

3) Create a repository to modularize your functionality, just like this one: 3)创建一个存储库来模块化你的功能,就像这样:

@Repository
public interface TransactionRepository extends JpaRepository<Transaction, Long> {
    Collection<Transaction> findAllByEmitter(Account emitter);
    Collection<Transaction> findAllByReceptor(Account receptor);
}

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

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