Skip to content

Instantly share code, notes, and snippets.

@jdesive
Last active October 8, 2017 00:55
Show Gist options
  • Save jdesive/2d2c9c233fc9720900ef1396f4ca8493 to your computer and use it in GitHub Desktop.
Save jdesive/2d2c9c233fc9720900ef1396f4ca8493 to your computer and use it in GitHub Desktop.
Help with Spring Boot JPA/Hibernate Entity mappings

Help with JPA/Hibernate in Spring Boot

Note: Using a Set<?> is the proper way of creating a list of objects. Using a List<?> and adding an item to the List, the entire List will be removed and recreated.

Creating an entity

@Entity
@Table(name = "foos")
@ToString // Lombok
public class Foo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int fooid;
    private String bar;
    
    public Foo(){
    }
    
    public Foo(String bar){
        this.bar = bar;
    }
    
    public int getId(){
        return this.fooid;
    }
    
    public void setId(int id){
        this.fooid = id;
    }
    
    public String getBar(){
        return this.bar;
    }
    
    public void setBar(String bar){
        this.bar = bar;
    }
    
}

Here we define a class to be an entity using the @Entity annotation. An entity requires a few things, an @Id, setter and getter methods, and a default constructor. Here we set the @Id to fooid and make it a auto-generated value. Then we crate the default constructor. Finally we create the setter and getter methods. These do not have to match the names above. Making fooid = id with getId() will name the field id in the JSON rather than fooid.

Note: I use the @Table annotation the specifiy the table name because I typically use Liquibase to create my tables

@OneToOne mapping

I have not found a use for this yet in my programs. Once I do I will update this page.

@OneToMany mapping

Foo.class

@JoinColumn(name = "foo")
@JsonIgnoreProperties("foo")
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private Set<Bar> bars;

Bar.class

@JoinColumn(name = "bars")
@JsonIgnoreProperties("bars")
@ManyToOne(cascade = CascadeType.ALL)
private Foo foo;

Here we define a variable called bars in our Foo.class entity. Then we set its JoinColumn to foo from our Bar.class entity and make sure our JSON response will ignore the foo variable to avoid cyclic errors.

Foo.class = Parent entity (One)

Bar.class = Child entity (Many)

@ManyToMany mapping

Foo.class

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

@JsonIgnoreProperties("foos")
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "foo_bars", joinColumns = @JoinColumn(name = "fooid"),
      inverseJoinColumns = @JoinColumn(name = "barid"))
private Set<Bar> bars;

Bar.class

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

@ManyToMany(mappedBy = "bars")
@JsonIgnoreProperties("bars")
private Set<Foo> foos;

Here we define a variable called bars in our Foo.class entity and a variable called foos in our Bar.class entity. Then we create a @JoinTable called foo_bars with the columns fooid, and barid to hold the mapping data.(This will create a new table with the name) Finally we make sure we avoid any cyclic errors by telling JSON to ignore the column from the other entity.

General Map mapping

@ElementCollection
@CollectionTable(name = "foo_bars", joinColumns = @JoinColumn(name = "fooid"))
@Column(name = "bar")
private Set<String> bars;

Here we define a variable called bars that will be a ElementCollection of strings. Then we create a new table to hold the data called foo_bars, settings the value to the foo's id variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment