One-way Relationship OneToMany

1

Hello everyone! Help me! I have a financial system and I am in doubt in the relationship of User and Account, according to UML. From what my teacher told me, here we have to do a OneToMany in User only, but I realized that account was left with a foreign key. Could you do what this relationship would be like? When I create the test I also create Account and hedge the data and then create User and user.setConta (account).

InUserIhavebelow:

@OneToMany(cascade=CascadeType.ALL,orphanRemoval=true)
@JoinColumn(name="ID_USER", foreingkey = @ForeingKey (name="USER_CONTA_FK))
private List < Account > accounts;

And in the class Account I did not define anything, even because they told me that I do not define, but this seems to me that something is missing and in SQL database DEVELOPER, the account table is with a column of ID_USUARY ... How? I did not put anything there ... ????

Thank you and sorry for any errors.

    
asked by anonymous 14.04.2017 / 03:22

1 answer

1

Your class diagram seems to me correct regarding the User relationship - > Account the foreign key MUST exist in your Account class since according to your system a user may have multiple accounts then in this case the Database needs to have a reference of which user is linked to that account . The unidirectional relationship will cause you to "access" the account through only the User class, since the mapping is done in this class, ie you will get a list of accounts that the user is linked to but not the other way round.

As your Account class is an entity, it must have JPA mapping with such an annotation to be mapped, but since the relationship you want is one-way, you do not have to ManyToOne in the Account class (which is the reverse of @OneToMany ).

If you have any further questions, I'm available!

    
14.04.2017 / 04:02