Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Last active April 26, 2017 04:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thjanssen/927f420f5cdfab495edc2066cc91c194 to your computer and use it in GitHub Desktop.
Save thjanssen/927f420f5cdfab495edc2066cc91c194 to your computer and use it in GitHub Desktop.
@Entity
public class Author {
@Id
private Long id;
}
<entity-mappings>
<entity class="org.thoughts.on.java.model.Author" name="Author">
<attributes>
<id name="id">
</id>
</attributes>
</entity>
</entity-mappings>
@Entity
@Table(name = “author”, schema = “bookstore”)
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = “author_generator”)
@SequenceGenerator(name=”author_generator”, sequenceName = “author_seq”)
@Column(name = “author_id”)
private Long id;
}
<entity-mappings>
<entity class="org.thoughts.on.java.model.Author" name="Author">
<table name="author" />
<schema name="bookstore" />
<attributes>
<id name="id">
<generated-value strategy="sequence" generator="author_generator"/>
<column name="author_id"/>
</id>
</attributes>
</entity>
<sequence-generator name="author_generator" sequence-name="author_seq"/>
</entity-mappings>
@SqlResultSetMapping(
name = "AuthorMapping",
entities = @EntityResult(
entityClass = Author.class,
fields = {
@FieldResult(name = "id", column = "authorId"),
@FieldResult(name = "name", column = "name")}))
<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings>
<entity class="org.thoughts.on.java.model.Author" name="Author">
...
<sql-result-set-mapping name="AuthorMappingXml">
<entity-result entity-class="org.thoughts.on.java.model.Author">
<field-result name="id" column="authorId" />
<field-result name="name" column="name" />
</entity-result>
</sql-result-set-mapping>
</entity>
...
</entity-mappings>
@Entity
@NamedEntityGraph(name = "graph.Author.books",
attributeNodes = @NamedAttributeNode("books"))
public class Author {
...
private List<Book> books = new ArrayList<Book>();
...
}
<entity-mappings>
<entity class="org.thoughts.on.java.model.Author" name="Author">
...
<named-entity-graph name="graph.Author.books">
<named-attribute-node name="books" />
</named-entity-graph>
</entity>
...
</entity-mappings>
@Entity
@NamedQuery(name = Author.QUERY_SELECT_BY_ID, query = “SELECT a FROM Author a WHERE a.id = :” + Author.PARAM_ID)
public class Author {
public static final String QUERY_SELECT_BY_ID = “Author.selectById”;
public static final String PARAM_ID = “id”;
}
<entity-mappings>
<entity class="org.thoughts.on.java.model.Author" name="Author">
...
<named-query name="Author.selectById">
<query><![CDATA[
SELECT a FROM Author a WHERE a.id = :id
]]></query>
</named-query>
</entity>
...
</entity-mappings>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="my-persistence-unit">
<description>Thougths on Java</description>
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<mapping-file>file:\\\C:\dev\wrk\XmlMapping\XmlMappings\myMappings.xml</mapping-file>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/test" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment