0

For example, I have database with table book (id, title, themeId, authorId), table author (id, name) and table theme (id, name). I dont know, should i create entity exactly like in database (and then i will have problems with join queries):

class Book {
    private id;
    String title;
    int bookThemeId;
    int bookAuthorId;
}

or create something like this:

class Book {
    private id;
    String title;
    BookTheme bookTheme;
    Author bookAuthor;
}

in second case i will have problems if authorId or themeId is null (it may be) and problems with getting from DAO in general, if i want get only books list (problems - fields is null, and very bulky builder for Book entity).

2
  • Second option would preclude books with multiple themes and authors. Commented Apr 3, 2020 at 7:37
  • @P.Salmon its not a big deal for this moment, its just example Commented Apr 3, 2020 at 7:49

2 Answers 2

2

I would go with the second option because is more object oriented. If you go with the first option, you will need to do a second query to get the details.

@Entity
@Table(name = "books")
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;
    //... 

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "theme_id", referencedColumnName = "id")
    private BookTheme bookTheme;

    // ... getters and setters
}

Your id will be null when fields are not created, right? For instance, when it is saved on database, it will always have an id.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for answer, yes i like second option too, but i dont use annotation and JPA for the time being learn servlets so i dont understand what exactly you mean by using annotations. About ids - i mean foreign id, like themeId, it may be null, so we can catch NullPointerException while using entities from second option.
I would not catch NullPointException instead just check if a variable if null or not. It is cleaner.
Ok, i think null-checking low price for easy entity using
1

I think you should go with 2nd approach. There will be no issue if your BookTheme r Author will be null if you use optional=true. For bulky builder, you can use fetchtype as lazy.

@Entity
@Table(name = "Book")
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional=true)
    @JoinColumn(name = "theme_id", referencedColumnName = "id")
    private BookTheme bookTheme;

    // ... getters and setters
}

Hope, This will work!

4 Comments

Thank you for answer, i get you, but i dont use annotations and JPA.
If you are not using that thn you should go with 1st approach
Why should i go with 1st option, if i dont use annotation?
If you go with 2nd option thn it will be bulky. In first option if you use primitive thn u can handle null thing and also get only id rather thn whole object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.