This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:vladmihalcea/hibernate-master-class
- Loading branch information
Showing
2 changed files
with
124 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
...m/vladmihalcea/hibernate/masterclass/laboratory/mapping/OneTableMultipleEntitiesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.vladmihalcea.hibernate.masterclass.laboratory.mapping; | ||
|
||
import com.vladmihalcea.hibernate.masterclass.laboratory.util.AbstractTest; | ||
import org.hibernate.annotations.DynamicUpdate; | ||
import org.hibernate.annotations.Immutable; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import javax.persistence.*; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
|
||
/** | ||
* OneTableMultipleEntitiesTest - Test to check the one table multiple entities | ||
* | ||
* @author Vlad Mihalcea | ||
*/ | ||
public class OneTableMultipleEntitiesTest extends AbstractTest { | ||
|
||
@Override | ||
protected Class<?>[] entities() { | ||
return new Class<?>[]{ | ||
Post.class, | ||
PostSummary.class, | ||
UpdatablePostSummary.class | ||
}; | ||
} | ||
|
||
@Before | ||
public void init() { | ||
super.init(); | ||
doInTransaction(session -> { | ||
Post post = new Post(); | ||
post.setName("Hibernate Master Class"); | ||
post.setDescription("Hibernate Master Class Description"); | ||
session.persist(post); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testOneTableMultipleEntities() { | ||
doInTransaction(session -> { | ||
Post post = (Post) session.get(Post.class, 1L); | ||
PostSummary postSummary = (PostSummary) session.get(PostSummary.class, 1L); | ||
UpdatablePostSummary updatablePostSummary = (UpdatablePostSummary) session.get(UpdatablePostSummary.class, 1L); | ||
assertEquals(post.getName(), postSummary.getName()); | ||
assertEquals(post.getName(), updatablePostSummary.getName()); | ||
updatablePostSummary.setName("Hibernate Master Class Tutorial."); | ||
}); | ||
} | ||
|
||
@Entity(name = "Post") | ||
public static class Post { | ||
|
||
@Id | ||
@GeneratedValue(strategy=GenerationType.AUTO) | ||
private Long id; | ||
|
||
private String name; | ||
|
||
private String description; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
} | ||
|
||
@Entity(name = "PostSummary") | ||
@Table(name = "Post") | ||
@Immutable | ||
public static class PostSummary { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private Long id; | ||
|
||
private String name; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} | ||
|
||
@Entity(name = "UpdatablePostSummary") | ||
@Table(name = "Post") | ||
@DynamicUpdate | ||
public static class UpdatablePostSummary { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private Long id; | ||
|
||
private String name; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
} | ||
|
||
} |