Spring Boot: 最佳的实体映射方式是什么?

huangapple go评论67阅读模式
英文:

Spring Boot: What is the best way to map entities

问题

Sure, here's the translation of the provided content:

我有两个实体 FlightBooking。我将尝试映射这两个实体。在第一种情况下,我将使用关系映射注解来映射这两个实体,这将与数据库创建紧密耦合。在第二种情况下,我将不使用关系映射注解来映射这两个实体。我认为这将与数据库创建松散耦合。我只想知道哪种方式是与映射一起工作的有效方式。

这是我的第一种情况:

实体

public class Flight
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long flightId;
    private Integer flightName;
    private Date departureTime;
    
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<Booking> bookings;

    // Getter, Setter, Constructors
}

public class Booking
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long bookingId;
    private Integer passengerName;
    private String passengerLocation;
    private Date bookingTime;
    
    @ManyToOne
    @JoinColumn(name = "flightId")
    private Flight flight;

    // Getter, Setter, Constructors
}

这是我的第二种情况:

实体

public class Flight
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long flightId;
    private Integer flightName;
    private Date departureTime;

    // Getter, Setter, Constructors
}

public class Booking
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long bookingId;
    private Integer passengerName;
    private String passengerLocation;
    private Date bookingTime;
    
    // 这里我们将引用Flight实体,但实际上并没有映射
    private Long flightId;

    // Getter, Setter, Constructors
}

我之所以提出这个问题,是因为我的一位资深同事告诉我,使用关系映射注解会导致应用程序加载时间较长。

英文:

I have two entities Flight and Booking. I will try to map this two entities. In a first case, I will map both entity with relational mapping annotations which will create a tight coupling with the database. In a second case, I will not map my both entity using relational mapping annotation. I think it will create a loose coupling with the database. I just wanted to know which is the efficient way to working with the mapping.

Here is my first case :

Entity

public class Flight
{
    @Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long flightId;
	private Integer flightName;
	private Date departureTime;
	
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	private Set&lt;Booking&gt; bookings;

    // Getter, Setter, Constructors
}

public class Booking
{
    @Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long bookingId;
	private Integer passangerName;
	private String passengerLocation;
	private Date bookingTime;
	
	@ManyToOne
	@JoinColumn(name = &quot;flightId&quot;)
	private Flight flight;

    // Getter, Setter, Constructors
}

Here is my second case :

Entity

public class Flight
{
    @Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long flightId;
	private Integer flightName;
	private Date departureTime;

    // Getter, Setter, Constructors
}

public class Booking
{
    @Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long bookingId;
	private Integer passangerName;
	private String passengerLocation;
	private Date bookingTime;
	
    // Here we will take a reference of Flight entity but it&#39;s not actully mapped
	private Long flightId;

    // Getter, Setter, Constructors
}

I am asking this question because, One of my senior told me to use relational mapping annotation will take much time to load your applicalication.

答案1

得分: 1

Sure, here is the translated content:

在第一种情况下,我将使用关系映射注解映射两个实体,这将与数据库产生紧密耦合。在第二种情况下,我将不使用关系映射注解映射我的两个实体。我认为这将与数据库产生松散耦合。我只想知道使用哪种方式与映射一起工作更有效。

在这两种情况下,您的代码都与数据库架构完全耦合。

使用 @OneToMany@ManyToOne 不会使您的代码更紧密地依赖于数据库。无论您选择哪种方式,您都有:

  • 为每个表的 @Entity 类,
  • 为每个列的属性。

也就是说,在Java中复制了关系架构。

这是完全可以接受的。为什么你的Java实体应该与关系模式首先解耦呢?

您有一个数据模型。关系模式是SQL中该模型的表示,而您的 @Entity 类是Java中该模型的表示。它们应该紧密耦合是很自然的。它们表示相同的东西。

而且,_ORM的整个目的_就是使它们易于耦合!

试图在不需要解耦的情况下随意“解耦”事物的开发人员会在后续产生臃肿、复杂和难以维护的问题。

我的一位资深同事告诉我使用关系映射注解会花费很多时间来加载应用程序。

我的意思是,没有经验的开发人员不应该发表像那样的愚蠢的笼统声明,除非有大量条件和警告,所以也许您误解了。

但无论如何,这是我的有条件和带条件的声明。

一般情况下,@OneToMany@ManyToOne 不会导致性能问题。 JPA 和 Hibernate 在全球范围内的数百万高性能系统中使用了二十多年。

当然,我可以轻松地提出(假设的或其他情况下)某些情况,其中可能希望避免使用关联,而更喜欢其他方法。并且对于经验不足的人来说,ORM中的关联映射确实带有一些怪癖和需要注意的地方。

但原则上不应该仅仅因为原则而避免关联映射。相反:理解它,并学会正确使用它。

英文:

> In a first case, I will map both entity with relational mapping annotations which will create a tight coupling with the database. In a second case, I will not map my both entity using relational mapping annotation. I think it will create a loose coupling with the database. I just wanted to know which is the efficient way to working with the mapping.

In both cases your code is completely coupled to the schema of your database.

Using @OneToMany and @ManyToOne doesn't make your code more closely dependent on the database. Whichever way you go, you have:

  • an @Entity class for each table, with
  • an attribute for each column.

That is, it's a copy in Java of the relational schema.

And that's perfectly fine. Why should your Java entities be decoupled from the relational schema in the first place?

You have a data model. The relational schema is a representation of that model in SQL, and your @Entity classes are a representation of that model in Java. It's perfectly natural that they should be coupled. They represent the same thing.

Hell, the whole point of ORM is to make it easy to couple them!

Developers who just try to arbitrarily "decouple" things that don't need to be decoupled create bloat and complexity and unmaintainability further down the line.

> One of my senior told me to use relational mapping annotation will take much time to load your application.

I mean, no experienced developer should be making a silly blanket statement like that, not without a whole lot of conditions and caveats, so perhaps you misunderstood.

But, anyway, here's my conditional and caveated statement.

No, it's not in general the case that @OneToMany and @ManyToOne cause performance problems. JPA and Hibernate are used in millions of highly-performing systems all over the world, for the past two decades.

Of course, I can easily come up with (contrived or otherwise) situations where one might want to avoid using associations in favor of something else. And certainly association mapping in ORM comes with some quirks and gotchas for the inexperienced.

But association mapping is not a thing you should just avoid on principle. Rather: understand it, and learn to use it properly.

huangapple
  • 本文由 发表于 2023年5月13日 22:04:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定