Skip to content

Instantly share code, notes, and snippets.

@spilth
Last active August 29, 2015 14:24
Show Gist options
  • Save spilth/e8f134913d6e4f062506 to your computer and use it in GitHub Desktop.
Save spilth/e8f134913d6e4f062506 to your computer and use it in GitHub Desktop.
Hibernate Cookbook

Hibernate Cookbook

Hexadecimal Date Field Error

Your member variable is using the class org.joda.time.DateTime instead of java.util.Date

Allow for Wrapping SQL Statements in import.sql

By default, Hibernate does not allow for SQL statements that span multiple lines.

To allow for this, add the following line to src/main/resources/hibernate.properties:

hibernate.hbm2ddl.import_files_sql_extractor=org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor

Store a Simple List

Use the ElementCollection annotation and set the fetch attribute to FetchType.EAGER in order to eagerly load a user's nick names when loading just the user:

@Entity
public class User {

    @ElementCollection(fetch = FetchType.EAGER)
    private List<String> nicknames = new ArrayList<>();

Then in your import.sql you can insert into user_nickname:

INSERT INTO user(id, name) VALUES (1, 'Nicholas');
INSERT INTO user_nicknames (user_id, nicknames)
  VALUES
    (1, 'Nick'),
    (1, 'Nickster');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment