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
@Entity
public class Author implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id = null;
@Version
@Column(name = "version")
@thjanssen
thjanssen / Author.java
Created April 19, 2015 02:50
JBoss Forge - Speedup your enterprise development - Part II RESTful Webservices (http://www.thoughts-on-java.org/2013/09/rapid-application-development-with.html)
@Entity
@XmlRootElement
public class Author implements Serializable
{
...
@XmlTransient
public Set<Book> getBooks()
{
return this.books;
@thjanssen
thjanssen / AuthorEndpointTest.java
Created April 19, 2015 02:59
JBoss Forge - Speedup your enterprise development - Part III Integration Tests with Arquillian (http://www.thoughts-on-java.org/2013/10/jboss-forge-speedup-your-enterprise.html)
@RunWith(Arquillian.class)
public class AuthorEndpointTest
{
@Inject
private AuthorEndpoint authorendpoint;
@Deployment
public static JavaArchive createDeployment()
{
@NamedQueries(@NamedQuery(name = "Trip.findByVehicle", query = "SELECT trip FROM Trip trip WHERE vehicle=:vehicle"))
// 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");
@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)
@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");
@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);
List<BookValue> results = ((Session)this.em.getDelegate()).createSQLQuery("SELECT b.id, b.title, b.version, a.firstName || ' ' || a.lastName as authorName FROM Book b JOIN Author a ON b.author_id = a.id")
.addScalar("id", StandardBasicTypes.LONG).addScalar("title").addScalar("version", StandardBasicTypes.LONG).addScalar("authorName")
.setResultTransformer(new AliasToBeanResultTransformer(BookValue.class)).list();
results.stream().forEach((book) -> {
System.out.println("Book: ID [" + book.getId() + "] title [" + book.getTitle() + "] authorName [" + book.getAuthorName() + "]");
});
// set input parameter
query.setParameter("x", 1.23d);
query.setParameter("y", 4.56d);
// call the stored procedure and get the result
query.execute();
Double sum = (Double) query.getOutputParameterValue("sum");