Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

java - What is difference between @Entity and @Embeddable

The difference between @Entity and @Embeddable annotation when each one is added before class declaration?

  1. the first create class as an entity, second insert column from another table?
  2. the first create class as an table, while second is embedded in another class?
  3. the first sets standard as a class, second define table type
  4. the first create table for that class, second embed something into different class
  5. the first define table property, second create union of two tables
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

@Entity annotation over a class defines that, it has a distinct separate existence. Thus we can run DB queries, without being dependent on any other class. @Embeddable annotation over a class defines that, it does not have independent existence. Thus we cannot run DB queries, without depending on other class. Here is an example to understand it better:

@Entity
User
  -- long id
  -- String name
  -- String email
     @Embedded
  -- UserDetails userDetail

@Embeddable
UserDetails
  -- Date dateOfBirth
  -- String sex
  -- String address
  -- String maritalStatus

Here you can see without having a User, UserDetails is useless.

Generally, in OOP, we first design the classes and then we design database entities. For some classes (like UserDetails class in the above example), we do not want to have separate tables in DB, where their independent existence is meaningless. In those cases, we mark the class as embeddable.

Typically, embeddable classes share the same table as the Entity in which they are embedded


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...