-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Bidirectional Circular Relationship
PostPosted: Thu Dec 03, 2015 9:52 am 
Newbie

Joined: Thu Dec 03, 2015 9:25 am
Posts: 1
Hello I am trying to implement a Bidirectional Circular Relationship with 2 @Entities in Apache Spring Boot.

For both entities I would like to be able to list anything linked to/from them.
For a Wallet I would like to list all associated transactions, for a transaction I would like to show the source and destination Wallets.
Most of the tutorials say that to implement a bidirectional relationship both entities need a List<?>, but this means to create a transaction I need to create it in the Transactions table, and add it to both wallets (3 operations), which could lead to inconsistencies in my data if i forget do perform all 3 operations (defeating the purpose of having a relationship in the first place).

Below I have provided a simple example in code and an image of the ERD.
I was hoping that an expert from the forums could be kind enough to help me.

Code:
public class Wallet {
   @Id
   @NotNull
   @GeneratedValue
   private Double walletid;
   private Double derivedAmount;
}

public class Transaction {
   @ManyToOne
   private Double sourceWallet;
   @ManyToOne
   private Double destWallet;
   private Double amount;
}


Image

Image


Top
 Profile  
 
 Post subject: Re: Bidirectional Circular Relationship
PostPosted: Thu Dec 03, 2015 10:41 am 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
User
Code:
@Entity
@Table(name = "USER")
public class User {
    @Id
    @Column(name = "USER_ID", nullable = false)
    private Integer id;

    @Column(name = "EMAIL", length = 100, unique = true, nullable = false)
    private String email;

    @Column(name = "NAME", length = 50, nullable = false)
    private String name;

    // It's a bad idea to use string password, please use binary password that is encrypted.
    // Of course, when save or update the data, Java application need encrypt the string password to binary password
    // by some algorithms, such as SHA1, MD5, RSA ...
    @Column(name = "PASSWORD", length = 20, nullable = false);
    private byte[] password;

    @OneToMany(mappedBy = "from") // Fake side, mappedBy Transaction.from
    private Set<Transaction> transactionsFromMe;

    @OneToMany(mappedBy = "to") // Fake side, mappedBy Transaction.to
    private Set<Transaction> transactionsToMe;

    public Set<Transaction> getTransactionsFromMe() {
         if (this.transactionsFromMe == null) {
             this.transactionsFromMe = new HashSet<>();
         }
         return this.transactionsFromMe;
    }

    public Set<Transaction> getTransactionsToMe() {
         if (this.transactionsToMe == null) {
             this.transactionsToMe = new HashSet<>();
         }
         return this.transactionsToMe;
    }

    Other getters and setters

}


Transaction
Code:
@Entity
@Table(name = "TRANSACTION")
public class Transaction {
    @Id
    @Column(name = "TRANSACTION_ID", nullable = false)
    private Integer id;

    @ManyToOne
    @JoinColumn(name = "FROM_ID", nullable = false)
    private User from; //Real side

    @ManyToOne
    @JoinColumn(name = "TO_ID", nullable = false)
    private User to; //Real side

    @Column(name = "TYPE", nullable = false)
    @Enumerated(EnumType.STRING)
    private TransactionType type;

    @Column(name = "DESCRIPTION", length = 1000, nullable = false)
    private String description;

    // It's a bad idea to use float or double for money
    // Use java.math.BigDecimal to specify the amount to make it is precise absolutely.
    @Column(name = "AMOUNT", nullable = false)
    private BigDecimal amount;

    Getters and setters

}


TransactionType
Code:
public enum TransactionType {
    ....
}


Two bidirectional associations are careted: One is combined by User.transactionFromMe and Transaction.from; the other one is combined by User.transactionsToMe and Transaction.to.

Note:
user.getTransactionsFromMe().add(transaction); // Change fake side, can not update relationship of database
transaction.setFrom(user); // Change real side, only this way can be used to update the relationship of database.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.