Y
Y
Yourmind2019-09-26 11:05:32
Java
Yourmind, 2019-09-26 11:05:32

What is the best way to form the Record class?

I have some kind of User class (I store it in the database). It has a primary key id. I also have a Record class, which means a client record. It has some fields. We also need to refer to the client. And here the question arises as to what is better: to store in the User class or only its id?
T e:
public class User {
private int id;
...
}
Option 1:
public class Record {
private int user;
...
}
option 2:
public class Record {
private User user;
...
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Victor Bomberow, 2019-09-26
@majstar_Zubr

If you have any doubts that in the future it will not be limited to the usual built-in type, then prefer the class.
If you are using the Record concept for the name, then most likely you have not yet decided on a class diagram, because Record is too high-level a concept, and most likely you plan to use it as a container class, which means it makes sense to make it a template / parameterizable storage type.
If you are still not sure, then it is better to leave User, because this way the intentions are expressed unambiguously, the code is clearer, it will be easier to refactor in the future

F
Frozen Coder, 2019-09-26
@frozen_coder

Are these classes a direct reflection of your database schema (that is, they describe with java code how your data is stored in the database) or are they your usual classes that participate in business logic?
If the former, then it is usually better to generate this automatically using some JOOQ, if you need it at all. And then it will be option 1.
If the second, then of course you choose option 2. What is the use of one id-shnik for you? And if you need to transfer Record somewhere, will you also drag its User separately?
There is another option that you have some kind of DTO and you only need to know the id, the associated user and that's it. You never want to see the user inside the Record, you don’t expect it, you don’t need it, it’s hard to carry extra ones. Then option 1.
In general. Proceed from the problem being solved and how you will continue to live and use it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question