One-way Onetomany relationship

Asked

Viewed 748 times

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 make a Onetomany in User only, but I realized that has got foreign key. Could you do what this relationship would be like? When I create the test I also create Account and Set the data and then create User and user.setConta(account). inserir a descrição da imagem aqui

In User I have below:

@Onetomany(Cascade=Cascadetype.ALL, orphanRemoval = true)
@Joincolumn(name= "ID_USER", foreingkey = @Foreingkey(name="USER_CONTA_FK))
Private list< Account > accounts;

And in the Account class I did not define anything, even because I was told that I do not define, but it seems to me that something is missing and in the SQL DEVELOPER database, the table has a column of ID_USUARIO... How? I didn’t put anything there... ????

Thank you and sorry for any mistake.

1 answer

1


Your class diagram seems correct in relation to the relationship User -> Account the foreign key MUST exist in its Class Bill as according to your system a user may own several accounts then in this case the Database needs to have a reference of which user is tied to that account. The unidirectional relationship will cause you to "access" the account through the class only User, since the mapping is done in this class, that is, you will be able to obtain a list of accounts that the user is linked to but not the other way around.

As your class Bill is an entity it must have the mapping in JPA with such annotation to be mapped, but as the relationship you want is unidirectional it is not necessary to do the @Manytoone in the Account class (which is the reverse of the @Onetomany).

If you have any other questions I’m available!

  • Hello, Matheus! Thank you! So my teacher was right :)... Sorry to disturb you, but if you can ask me one more question or more :). The first is When I create an account test, create a bank and add in account and in the SQL database Develope the key FK ID_USUARIO FICA null, right?

  • Manda Rafael !

  • I’m having trouble understanding the issue of add account. How do I do it: Bank bankDAO = new Bank(); Bank = new Bank(); Bank = bank.lookId(1L); And then I’ll do it.setBanco(bank); Are you right? and the method is in the next comment.

  • public Entity searchId(Long id) { Try { Query query = getSessao() . createQuery("Select entity from " + class.getSimpleName() + " entity Where entity.id=:id"); query.setParameter("id", id); Entity result = null; if (!query.getResultList().isEmpty()) { result = (Entity) query.getResultList(). get(0); } Return result; } catch (Runtimeexception error) { throw error; } Finally { sessao.close(); } }

  • I don’t usually use this "concatenation" method to create my querys. JPA gives you the possibility to use named querys that are defined querys statically, this does not prevent you to pass parameters for these queries,I recommend you study about Named Query and Facades, which facilitates the organization of your classes for data access. I highly recommend the blog uaihebert.com that addresses the subject very well. For further clarification on query’s I recommend asking a new question about this. I hope I helped, don’t forget to mark as "Answer" if I helped you with this question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.