Leonardo Manzini
Leonardo Manzini

Reputation: 87

Hibernate Entity Mapping

Good night everyone,

I want to model a database that has the following entities and their perspective relationships:

uml-relationship

But everytime I run the Java project to create the model at database, what I create is something like this:

database-articles-table

There is another way to map this relationship? I'm mapping like that:

Article entity:

@Entity
public class Article {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private Boolean featured;

    @Column(nullable = false)
    private String title;

    @Column(nullable = false)
    private String url;

    @Column(name = "image_url", nullable = false)
    private String imageUrl;

    @Column(name = "news_site", nullable = false)
    private String newsSite;

    @Column(nullable = false)
    private String summary;

    @Column(name = "published_at", nullable = false)
    private String publishedAt;

    @OneToMany
    @JoinColumn(name = "launches_id")
    private List<Launches> launches;

    @OneToMany
    @JoinColumn(name = "events_id")
    private  List<Events> events;
}

Launches entity

@Entity
public class Launches {
    @Id
    private String id;

    private String provider;
}

Events entity:

@Entity
public class Events {
    @Id
    private Long id;

    private String provider;
}

And I want to map this JSON, with this same launcher and events appearing in other articles:

{
    "id": 4278,
    "title": "GAO warns of more JWST delays",
    "url": "https://spacenews.com/gao-warns-of-more-jwst-delays/",
    "imageUrl": "https://spacenews.com/wp-content/uploads/2019/08/jwst-assembled.jpg",
    "newsSite": "SpaceNews",
    "summary": "",
    "publishedAt": "2020-01-28T23:25:02.000Z",
    "updatedAt": "2021-05-18T13:46:00.284Z",
    "featured": false,
    "launches": [
        {
            "id": "d0fa4bb2-80ea-4808-af08-7785dde53bf6",
            "provider": "Launch Library 2"
        }
    ],
    "events": []
},
{
    "id": 4304,
    "title": "Government watchdog warns of another JWST launch delay",
    "url": "https://spaceflightnow.com/2020/01/30/government-watchdog-warns-of-another-jwst-launch-delay/",
    "imageUrl": "https://mk0spaceflightnoa02a.kinstacdn.com/wp-content/uploads/2020/01/48936479373_2d8a120c8e_k.jpg",
    "newsSite": "Spaceflight Now",
    "summary": "",
    "publishedAt": "2020-01-30T04:08:00.000Z",
    "updatedAt": "2021-05-18T13:46:01.640Z",
    "featured": false,
    "launches": [
        {
            "id": "d0fa4bb2-80ea-4808-af08-7785dde53bf6",
            "provider": "Launch Library 2"
        }
    ],
    "events": []
}

Upvotes: 2

Views: 576

Answers (1)

xerx593
xerx593

Reputation: 13261

According to your diagram, it should be:

    @ManyToOne
    @JoinColumn(name = "launches_id")
    private Launches launches;

    @ManyToOne
    @JoinColumn(name = "events_id")
    private  Events events;

...and not @OneToMany ;) (Can there be an "Article" (with id=x) having launchers_id=y AND launchers_id=z? No, vice versa!:)

...for the @OneToMany, you should find the join columns "on the other side" (of relationship(s)).


According to your JSON, it is OneToMany. But then, we have to draw/expect:

@Entity
class Article {
    //... id, columns, blah
    @OneToMany
    @JoinColumn(name = "article_id")  // Launches "owns the relationship"/column 
    private List<Launches> launches;

    @OneToMany
    @JoinColumn(name = "article_id")  // Events...!
    private  List<Events> events;
}

Generally, (when you expose your db model via json,) ensure:

  • no "circles" (in bi-directional associations). (@JsonManagedReference, @JsonBackReference, @JsonIgnoreProperties, ... )

  • not to expose data, that you don't want to expose. (@JsonIgnoreProperties, ...)

Regarding Hibernate-ManyToOne, please refer to https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/

Regarding , best to:

Upvotes: 3

Related Questions