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).
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.
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?
– Rafael Blum
Manda Rafael !
– mattphilipe94
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.
– Rafael Blum
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(); } }
– Rafael Blum
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.
– mattphilipe94