Hibernate 实体映射
Hibernate Entity Mapping
大家晚安,
我想对具有以下实体及其透视关系的数据库建模:
但每次我 运行 Java 项目在数据库中创建模型时,我创建的是这样的:
还有另一种映射这种关系的方法吗?我是这样映射的:
文章实体:
@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;
}
启动实体
@Entity
public class Launches {
@Id
private String id;
private String provider;
}
事件实体:
@Entity
public class Events {
@Id
private Long id;
private String provider;
}
我想将此 JSON 映射到其他文章中出现的相同启动器和事件:
{
"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": []
}
根据你的图表,应该是:
@ManyToOne
@JoinColumn(name = "launches_id")
private Launches launches;
@ManyToOne
@JoinColumn(name = "events_id")
private Events events;
...而不是 @OneToMany
;)(可以有一个“文章”(id=x)具有 launchers_id=y AND launchers_id=z 吗?不,反之亦然!:)
...对于 @OneToMany
,您应该在(关系的)“另一侧”找到连接列。
根据您的JSON,是OneToMany
。但是,我们必须 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;
}
通常,(当您通过 json 公开您的数据库模型时)确保:
没有“圈子”(在双向关联中)。 (@JsonManagedReference, @JsonBackReference, @JsonIgnoreProperties
, ... )
不公开您不想公开的数据。 (@JsonIgnoreProperties
, ...)
关于Hibernate-ManyToOne,请参考https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/
关于spring-data-jpa,最好:
大家晚安,
我想对具有以下实体及其透视关系的数据库建模:
但每次我 运行 Java 项目在数据库中创建模型时,我创建的是这样的:
还有另一种映射这种关系的方法吗?我是这样映射的:
文章实体:
@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;
}
启动实体
@Entity
public class Launches {
@Id
private String id;
private String provider;
}
事件实体:
@Entity
public class Events {
@Id
private Long id;
private String provider;
}
我想将此 JSON 映射到其他文章中出现的相同启动器和事件:
{
"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": []
}
根据你的图表,应该是:
@ManyToOne
@JoinColumn(name = "launches_id")
private Launches launches;
@ManyToOne
@JoinColumn(name = "events_id")
private Events events;
...而不是 @OneToMany
;)(可以有一个“文章”(id=x)具有 launchers_id=y AND launchers_id=z 吗?不,反之亦然!:)
...对于 @OneToMany
,您应该在(关系的)“另一侧”找到连接列。
根据您的JSON,是OneToMany
。但是,我们必须 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;
}
通常,(当您通过 json 公开您的数据库模型时)确保:
没有“圈子”(在双向关联中)。 (
@JsonManagedReference, @JsonBackReference, @JsonIgnoreProperties
, ... )不公开您不想公开的数据。 (
@JsonIgnoreProperties
, ...)
关于Hibernate-ManyToOne,请参考https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/
关于spring-data-jpa,最好: