Skip to content

Instantly share code, notes, and snippets.

View thjanssen's full-sized avatar
🎓
Creating the best Java persistence courses for the @Persistence-Hub

Thorben Janssen thjanssen

🎓
Creating the best Java persistence courses for the @Persistence-Hub
View GitHub Profile
@SqlResultSetMapping(
name = "AuthorMapping",
entities = @EntityResult(
entityClass = Author.class,
fields = {
@FieldResult(name = "id", column = "authorId"),
@FieldResult(name = "firstName", column = "firstName"),
@FieldResult(name = "lastName", column = "lastName"),
@FieldResult(name = "version", column = "version")}))
@thjanssen
thjanssen / Product.java
Last active October 13, 2017 14:42
How to activate Hibernate Statistics to analyze performance issues (http://www.thoughts-on-java.org/2015/03/how-to-activate-hibernate-statistics-to.html)
@Entity
public class Product implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id = null;
@Version
@Column(name = "version")
@Entity
@NamedEntityGraph(name = "graph.Order.items",
attributeNodes = @NamedAttributeNode("items"))
public class Order implements Serializable {
....
@Singleton
@Remote(SingletonRemote.class)
public class DefaultLock implements SingletonRemote {
Logger logger = Logger.getLogger(DefaultLock.class.getName());
private int counter = 0;
@Override
public void method1() {
this.logger.info("method1: " + counter);
@thjanssen
thjanssen / CryptoConverter.java
Created April 19, 2015 03:43
Testing with Aliens: How to test a JPA type converter with Arquillian (http://www.thoughts-on-java.org/2014/06/testing-with-aliens-how-to-test-jpa.html)
@Converter
public class CryptoConverter implements AttributeConverter<String, String> {
private static final String ALGORITHM = "AES/ECB/PKCS5Padding";
private static final byte[] KEY = "MySuperSecretKey".getBytes();
@Override
public String convertToDatabaseColumn(String ccNumber) {
// do some encryption
Key key = new SecretKeySpec(KEY, "AES");
@Entity
public class CreditCard {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String ccNumber;
private String name;
@thjanssen
thjanssen / Order.java
Created April 19, 2015 03:36
JPA 2.1 Entity Graph - Part 2: Define lazy/eager loading at runtime (http://www.thoughts-on-java.org/2014/04/jpa-21-entity-graph-part-2-define.html)
@Entity
@Table(name = "purchaseOrder")
@NamedEntityGraph(name = "graph.Order.items",
attributeNodes = @NamedAttributeNode(value = "items", subgraph = "items"),
subgraphs = @NamedSubgraph(name = "items", attributeNodes = @NamedAttributeNode("product")))
public class Order implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
@Entity
@Table(name = "purchaseOrder")
@NamedEntityGraph(name = "graph.Order.items",
attributeNodes = @NamedAttributeNode(value = "items", subgraph = "items"),
subgraphs = @NamedSubgraph(name = "items", attributeNodes = @NamedAttributeNode("product")))
public class Order implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
// define EJB client properties
final Properties props = new Properties();
// define SSL encryption
props.put("remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED",
"true");
props.put("remote.connection.default.connect.options.org.xnio.Options.SSL_STARTTLS",
"true");
// connection properties
props.put("remote.connections", "default");
props.put("remote.connection.default.host", "localhost");
//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.4-2
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2014.01.13 at 07:38:24 PM CET
//
package blog.thoughts.on.java;