Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Last active November 20, 2018 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save thjanssen/086b64c636e338fb0b1d72461c14042a to your computer and use it in GitHub Desktop.
Save thjanssen/086b64c636e338fb0b1d72461c14042a to your computer and use it in GitHub Desktop.
@Entity
public class Author {
@ElementCollection
@MapKeyColumn(name = "address_type")
@MapKeyEnumerated(EnumType.STRING)
private Map<AddressType, Address>address = new HashMap<AddressType, Address>();
...
}
@Entity
public class Author {
@ManyToMany
@JoinTable(
name="AuthorBookGroup",
joinColumns={@JoinColumn(name="fk_author", referencedColumnName="id")},
inverseJoinColumns={@JoinColumn(name="fk_group", referencedColumnName="id")})
@MapKey(name = "title")
private Map<String, BookGroup> bookGroups = new HashMap<String, BookGroup>();
...
}
@Entity
public class BookGroup {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@OneToMany(mappedBy = "group")
private List<Book> books = new ArrayList<Book>();
private String title;
...
}
Author a = new Author();
a.setFirstName("Thorben");
a.setLastName("Janssen");
em.persist(a);
Book b = new Book();
b.setTitle("Hibernate Tips");
b.setFormat(Format.PAPERBACK);
b.getAuthors().add(a);
em.persist(b);
a.getBooks().put(b.getTitle(), b);
TypedQuery<Author> q = em.createQuery("SELECT a FROM Author a JOIN a.books b WHERE b.title LIKE :title", Author.class);
q.setParameter("title", "%Hibernate Tips%");
a = q.getSingleResult();
TypedQuery<Author> q = em.createQuery("SELECT a FROM Author a JOIN a.books b WHERE KEY(b) LIKE :title ", Author.class);
q.setParameter("title", "%Hibernate Tips%");
a = q.getSingleResult();
@Entity
public class Author {
@ManyToMany
@JoinTable(
name="BookAuthor",
joinColumns={@JoinColumn(name="bookId", referencedColumnName="id")},
inverseJoinColumns={@JoinColumn(name="authorId", referencedColumnName="id")})
private List<Book> books = new ArrayList<Book>();
...
}
@Entity
public class Author {
@ManyToMany
@JoinTable(
name="AuthorBookGroup",
joinColumns={@JoinColumn(name="fk_author", referencedColumnName="id")},
inverseJoinColumns={@JoinColumn(name="fk_group", referencedColumnName="id")})
@MapKey(name = "title")
private Map<String, Book> books = new HashMap<String, Book>();
...
}
@BondarenkoIlya
Copy link

Hello! Could you help me ,I found entity structure here https://www.thoughts-on-java.org/map-association-java-util-map/ but where can I find structure of database tables ?

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