Skip to content
This repository has been archived by the owner on Jan 5, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' of github.com:vladmihalcea/hibernate-master-class
Browse files Browse the repository at this point in the history
  • Loading branch information
vladmihalcea committed Mar 14, 2015
2 parents adb7628 + b8f7050 commit 23cd89e
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @author Vlad Mihalcea
*/
public class ManyToManyCascadeDoneRightTest extends AbstractTest {
public class ManyToManyCascadeDissociationTest extends AbstractTest {

@Override
protected Class<?>[] entities() {
Expand Down Expand Up @@ -58,6 +58,7 @@ public void testCascadeTypeDelete() {
Author _Mark_Armstrong = getByName(session, "Mark Armstrong");
_Mark_Armstrong.remove();
session.delete(_Mark_Armstrong);
session.flush();
Author _John_Smith = getByName(session, "John Smith");
assertEquals(2, _John_Smith.books.size());
});
Expand All @@ -66,13 +67,11 @@ public void testCascadeTypeDelete() {

private Author getByName(Session session, String fullName) {
return (Author) session
.createQuery("select a from Author a where a.fullName = :fullName")
.createQuery("select a from Author a join fetch a.books where a.fullName = :fullName")
.setParameter("fullName", fullName)
.uniqueResult();
}



@Entity(name = "Author")
public static class Author {

Expand Down
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;
}
}

}

0 comments on commit 23cd89e

Please sign in to comment.