Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
hibernate: added intermediary table
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioCodes committed Jun 12, 2019
1 parent 069c83c commit 3bbb4df
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Hobby {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private Long id;

private String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import java.util.List;

@Table
@Entity
Expand All @@ -17,10 +20,13 @@ public class Person {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private Long id;

private String name;

private Integer age;

@ManyToMany(cascade = CascadeType.PERSIST)
private List<Hobby> hobbies;

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package es.msanchez.frameworks.java.spring.boot.hibernate;

import es.msanchez.frameworks.java.spring.boot.entity.Hobby;
import es.msanchez.frameworks.java.spring.boot.entity.Person;
import org.hibernate.Session;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;

public class HibernateUtilTest {

private Session session;
Expand All @@ -21,7 +25,7 @@ public void tearDown() {
}

@Test
public void testInsert() {
public void testInsertHobby() {
// @GIVEN
session.beginTransaction();

Expand All @@ -34,4 +38,27 @@ public void testInsert() {

// @THEN
}

@Test
public void testInsertPerson() {
// @GIVEN
session.beginTransaction();

final List<Hobby> hobbies = new ArrayList<>();
final Hobby hobby = new Hobby();
hobby.setName("Climbing");
hobbies.add(hobby);

final Person person = new Person();
person.setName("Mario");
person.setAge(1);
person.setHobbies(hobbies);

// @WHEN
session.save(hobby);
session.save(person);
session.getTransaction().commit();

// @THEN
}
}

0 comments on commit 3bbb4df

Please sign in to comment.