Skip to content

Commit 82a183b

Browse files
author
Nicolas Romanetti
committed
some usage example
1 parent 9108bf7 commit 82a183b

File tree

11 files changed

+451
-0
lines changed

11 files changed

+451
-0
lines changed

pom.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,26 @@
252252
<artifactId>aspectjweaver</artifactId>
253253
<version>1.8.7</version>
254254
</dependency>
255+
<!-- test dependencies) -->
256+
<dependency>
257+
<groupId>com.h2database</groupId>
258+
<artifactId>h2</artifactId>
259+
<version>1.4.190</version>
260+
<scope>test</scope>
261+
</dependency>
262+
<dependency>
263+
<groupId>junit</groupId>
264+
<artifactId>junit</artifactId>
265+
<version>4.12</version>
266+
<scope>test</scope>
267+
</dependency>
268+
<dependency>
269+
<groupId>org.springframework</groupId>
270+
<artifactId>spring-test</artifactId>
271+
<version>4.2.2.RELEASE</version>
272+
<scope>test</scope>
273+
</dependency>
274+
255275
</dependencies>
256276
<build>
257277
<plugins>

src/test/java/demo/Account.java

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Source code generated by Celerio, a Jaxio product.
3+
* Documentation: http://www.jaxio.com/documentation/celerio/
4+
* Follow us on twitter: @jaxiosoft
5+
* Need commercial support ? Contact us: info@jaxio.com
6+
* Template pack-backend-jpa:src/main/java/domain/Entity.e.vm.java
7+
* Template is part of Open Source Project: https://github.com/jaxio/pack-backend-jpa
8+
*/
9+
package demo;
10+
11+
import com.google.common.base.MoreObjects;
12+
import com.google.common.base.Objects;
13+
import com.jaxio.jpa.querybyexample.Identifiable;
14+
import org.hibernate.annotations.GenericGenerator;
15+
import org.slf4j.Logger;
16+
import org.slf4j.LoggerFactory;
17+
18+
import javax.persistence.*;
19+
import java.io.Serializable;
20+
import java.util.Date;
21+
22+
import static javax.persistence.TemporalType.TIMESTAMP;
23+
24+
@Entity
25+
@Table(name = "ACCOUNT")
26+
public class Account implements Identifiable<String>, Serializable {
27+
private static final long serialVersionUID = 1L;
28+
private static final Logger log = LoggerFactory.getLogger(Account.class);
29+
30+
// Raw attributes
31+
private String id;
32+
private String username;
33+
private Date birthDate;
34+
35+
@Override
36+
@Column(name = "ID", length = 36)
37+
@GeneratedValue(generator = "strategy-uuid2")
38+
@GenericGenerator(name = "strategy-uuid2", strategy = "uuid2")
39+
@Id
40+
public String getId() {
41+
return id;
42+
}
43+
44+
@Override
45+
public void setId(String id) {
46+
this.id = id;
47+
}
48+
49+
public Account id(String id) {
50+
setId(id);
51+
return this;
52+
}
53+
54+
@Override
55+
@Transient
56+
public boolean isIdSet() {
57+
return id != null && !id.isEmpty();
58+
}
59+
60+
// -- [username] ------------------------
61+
62+
@Column(name = "USERNAME", nullable = false, unique = true, length = 100)
63+
public String getUsername() {
64+
return username;
65+
}
66+
67+
public void setUsername(String username) {
68+
this.username = username;
69+
}
70+
71+
public Account username(String username) {
72+
setUsername(username);
73+
return this;
74+
}
75+
// -- [birthDate] ------------------------
76+
77+
@Column(name = "BIRTH_DATE", length = 23)
78+
@Temporal(TIMESTAMP)
79+
public Date getBirthDate() {
80+
return birthDate;
81+
}
82+
83+
public void setBirthDate(Date birthDate) {
84+
this.birthDate = birthDate;
85+
}
86+
87+
public Account birthDate(Date birthDate) {
88+
setBirthDate(birthDate);
89+
return this;
90+
}
91+
92+
/**
93+
* Equals implementation using a business key.
94+
*/
95+
@Override
96+
public boolean equals(Object other) {
97+
return this == other || (other instanceof Account && hashCode() == other.hashCode());
98+
}
99+
100+
private volatile int previousHashCode = 0;
101+
102+
@Override
103+
public int hashCode() {
104+
int hashCode = Objects.hashCode(getUsername());
105+
106+
if (previousHashCode != 0 && previousHashCode != hashCode) {
107+
log.warn("DEVELOPER: hashCode has changed!." //
108+
+ "If you encounter this message you should take the time to carefully review equals/hashCode for: " //
109+
+ getClass().getCanonicalName());
110+
}
111+
112+
previousHashCode = hashCode;
113+
return hashCode;
114+
}
115+
116+
/**
117+
* Construct a readable string representation for this Account instance.
118+
*
119+
* @see Object#toString()
120+
*/
121+
@Override
122+
public String toString() {
123+
return MoreObjects.toStringHelper(this) //
124+
.add("id", getId()) //
125+
.add("username", getUsername()) //
126+
.add("birthDate", getBirthDate()) //
127+
.toString();
128+
}
129+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package demo;
2+
3+
import com.jaxio.jpa.querybyexample.SearchParameters;
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.test.annotation.Rollback;
8+
import org.springframework.test.context.ContextConfiguration;
9+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
10+
import org.springframework.transaction.annotation.Transactional;
11+
12+
import javax.inject.Inject;
13+
import java.text.DateFormat;
14+
import java.text.SimpleDateFormat;
15+
import java.util.Date;
16+
import java.util.List;
17+
18+
import static org.hamcrest.CoreMatchers.is;
19+
20+
@RunWith(SpringJUnit4ClassRunner.class)
21+
@ContextConfiguration(locations = {"classpath*:applicationContext.xml"})
22+
@Transactional
23+
public class AccountQueryByExampleTest {
24+
@Inject
25+
AccountRepository accountRepository;
26+
27+
@Test
28+
@Rollback
29+
public void dateRangeQuery() throws Exception {
30+
31+
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
32+
Date from = dateFormat.parse("1920-12-01");
33+
Date to = dateFormat.parse("1974-12-01");
34+
35+
SearchParameters sp = new SearchParameters().range(from, to, Account_.birthDate);
36+
List<Account> accountList = accountRepository.find(sp);
37+
Assert.assertThat(accountList.size(), is(1));
38+
Assert.assertThat(accountList.get(0).getUsername(), is("nico"));
39+
System.out.println("******************************************");
40+
System.out.println(accountList.get(0));
41+
System.out.println("******************************************");
42+
}
43+
44+
@Test
45+
@Rollback
46+
public void dateRangeAndLikeQuery() throws Exception {
47+
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
48+
Date from = dateFormat.parse("1920-12-01");
49+
Date to = dateFormat.parse("2020-12-01");
50+
51+
SearchParameters sp = new SearchParameters().range(from, to, Account_.birthDate);
52+
sp.anywhere(); // enable search like '%...%'
53+
54+
Account example = new Account();
55+
example.setUsername("i"); // will search username Like '%i%'
56+
57+
List<Account> accountList = accountRepository.find(example, sp);
58+
System.out.println("******************************************");
59+
for (Account a : accountList) {
60+
System.out.println(a);
61+
}
62+
System.out.println("******************************************");
63+
}
64+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Source code generated by Celerio, a Jaxio product.
3+
* Documentation: http://www.jaxio.com/documentation/celerio/
4+
* Follow us on twitter: @jaxiosoft
5+
* Need commercial support ? Contact us: info@jaxio.com
6+
* Template pack-backend-jpa:src/main/java/repository/Repository.e.vm.java
7+
* Template is part of Open Source Project: https://github.com/jaxio/pack-backend-jpa
8+
*/
9+
package demo;
10+
11+
import com.jaxio.jpa.querybyexample.GenericRepository;
12+
import org.springframework.transaction.annotation.Transactional;
13+
14+
import javax.inject.Named;
15+
import javax.inject.Singleton;
16+
17+
/**
18+
* {@link GenericRepository} for {@link Account}
19+
*/
20+
@Named
21+
@Singleton
22+
public class AccountRepository extends GenericRepository<Account, String> {
23+
24+
public AccountRepository() {
25+
super(Account.class);
26+
}
27+
28+
@Override
29+
public Account getNew() {
30+
return new Account();
31+
}
32+
33+
/**
34+
* Return the persistent instance of {@link Account} with the given unique property value username,
35+
* or null if there is no such persistent instance.
36+
*
37+
* @param username the unique value
38+
* @return the corresponding {@link Account} persistent instance or null
39+
*/
40+
@Transactional(readOnly = true)
41+
public Account getByUsername(String username) {
42+
return findUniqueOrNone(new Account().username(username));
43+
}
44+
45+
46+
}

src/test/java/demo/Account_.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Source code generated by Celerio, a Jaxio product.
3+
* Documentation: http://www.jaxio.com/documentation/celerio/
4+
* Follow us on twitter: @jaxiosoft
5+
* Need commercial support ? Contact us: info@jaxio.com
6+
* Template pack-backend-jpa:src/main/java/domain/EntityMeta_.e.vm.java
7+
* Template is part of Open Source Project: https://github.com/jaxio/pack-backend-jpa
8+
*/
9+
package demo;
10+
11+
import javax.persistence.metamodel.SingularAttribute;
12+
import javax.persistence.metamodel.StaticMetamodel;
13+
import java.util.Date;
14+
15+
@StaticMetamodel(Account.class)
16+
public abstract class Account_ {
17+
18+
// Raw attributes
19+
public static volatile SingularAttribute<Account, String> id;
20+
public static volatile SingularAttribute<Account, String> username;
21+
public static volatile SingularAttribute<Account, Date> birthDate;
22+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Source code generated by Celerio, a Jaxio product.
3+
* Documentation: http://www.jaxio.com/documentation/celerio/
4+
* Follow us on twitter: @jaxiosoft
5+
* Need commercial support ? Contact us: info@jaxio.com
6+
* Template pack-backend-jpa:src/main/java/domain/support/IdentifiableHashBuilder.p.vm.java
7+
* Template is part of Open Source Project: https://github.com/jaxio/pack-backend-jpa
8+
*/
9+
package demo;
10+
11+
import com.jaxio.jpa.querybyexample.Identifiable;
12+
import org.slf4j.Logger;
13+
14+
import java.io.Serializable;
15+
16+
/**
17+
* The first time the {@link #hash(Logger, Identifiable)} is called, we check if the primary key is present or not.
18+
* <ul>
19+
* <li>If yes: we use it to get the hash</li>
20+
* <li>If no: we use a VMID during the entire life of this instance even if later on this instance is assigned a primary key.</li>
21+
* </ul>
22+
*/
23+
public class IdentifiableHashBuilder implements Serializable {
24+
private static final long serialVersionUID = 1L;
25+
private Object technicalId;
26+
27+
public int hash(Logger log, Identifiable<?> identifiable) {
28+
if (technicalId == null) {
29+
if (identifiable.isIdSet()) {
30+
technicalId = identifiable.getId();
31+
} else {
32+
technicalId = new java.rmi.dgc.VMID();
33+
log.warn("DEVELOPER: hashCode is not safe." //
34+
+ "If you encounter this message you should take the time to carefully " //
35+
+ "review the equals/hashCode methods for: " + identifiable.getClass().getCanonicalName() //
36+
+ " You may consider using a business key.");
37+
}
38+
}
39+
return technicalId.hashCode();
40+
}
41+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Source code generated by Celerio, a Jaxio product.
3+
* Documentation: http://www.jaxio.com/documentation/celerio/
4+
* Follow us on twitter: @jaxiosoft
5+
* Need commercial support ? Contact us: info@jaxio.com
6+
* Template pack-backend-jpa:src/main/java/configuration/JpaConfiguration.p.vm.java
7+
* Template is part of Open Source Project: https://github.com/jaxio/pack-backend-jpa
8+
*/
9+
package demo;
10+
11+
import org.hibernate.SessionFactory;
12+
import org.hibernate.jpa.HibernateEntityManagerFactory;
13+
import org.springframework.beans.factory.annotation.Value;
14+
import org.springframework.context.annotation.Bean;
15+
import org.springframework.context.annotation.Configuration;
16+
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
17+
import org.springframework.orm.jpa.JpaTransactionManager;
18+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
19+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
20+
21+
import javax.annotation.Resource;
22+
import javax.sql.DataSource;
23+
import java.util.Properties;
24+
25+
@Configuration
26+
public class JpaConfiguration {
27+
28+
@Resource(name = "dataSource")
29+
private DataSource dataSource;
30+
31+
/**
32+
* Enable exception translation for beans annotated with @Repository
33+
*/
34+
@Bean
35+
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
36+
return new PersistenceExceptionTranslationPostProcessor();
37+
}
38+
39+
/**
40+
* @see http://www.springframework.org/docs/reference/transaction.html
41+
*/
42+
@Bean
43+
public JpaTransactionManager transactionManager() {
44+
return new JpaTransactionManager();
45+
}
46+
47+
/**
48+
* Build the entity manager with Hibernate as a provider.
49+
*/
50+
@Bean
51+
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
52+
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
53+
emf.setDataSource(dataSource);
54+
// We set the persistenceXmlLocation to a different name to make it work on JBoss.
55+
emf.setPersistenceXmlLocation("classpath:META-INF/persistence.xml");
56+
emf.setPersistenceUnitName("demoPU");
57+
emf.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
58+
return emf;
59+
}
60+
}

0 commit comments

Comments
 (0)