Skip to content

Instantly share code, notes, and snippets.

@thjanssen
Created February 7, 2017 09:18
Show Gist options
  • Save thjanssen/4ade9ab30dc4cb0a7a0bc37e7f7da597 to your computer and use it in GitHub Desktop.
Save thjanssen/4ade9ab30dc4cb0a7a0bc37e7f7da597 to your computer and use it in GitHub Desktop.
// Transaction 1: Check that no tweet matches the search string
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
FullTextEntityManager fullTextEm = Search.getFullTextEntityManager(em);
QueryBuilder tweetQb = fullTextEm.getSearchFactory().buildQueryBuilder().forEntity(Tweet.class).get();
Query fullTextQuery = tweetQb.keyword().onField(Tweet_.message.getName()).matching(“Message updated”).createQuery();
List results = fullTextEm.createFullTextQuery(fullTextQuery).getResultList();
Assert.assertEquals(0, results.size());
em.getTransaction().commit();
em.close();
// Transaction 2: Update a tweet
em = emf.createEntityManager();
em.getTransaction().begin();
Tweet tweet = em.find(Tweet.class, 1L);
tweet.setMessage(“Message updated – “+tweet.getMessage());
em.getTransaction().commit();
em.close();
// Transaction 3: Check that 1 tweet matches the search string
em = emf.createEntityManager();
em.getTransaction().begin();
fullTextEm = Search.getFullTextEntityManager(em);
tweetQb = fullTextEm.getSearchFactory().buildQueryBuilder().forEntity(Tweet.class).get();
fullTextQuery = tweetQb.keyword().onField(Tweet_.message.getName()).matching(“Message updated”).createQuery();
results = fullTextEm.createFullTextQuery(fullTextQuery).getResultList();
Assert.assertEquals(1, results.size());
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
FullTextEntityManager fullTextEm = Search.getFullTextEntityManager(em);
QueryBuilder tweetQb = fullTextEm.getSearchFactory().buildQueryBuilder().forEntity(Tweet.class).get();
Query fullTextQuery = tweetQb.keyword().onField(Tweet_.message.getName()).matching(“validate Hibernate”).createQuery();
List results = fullTextEm.createFullTextQuery(fullTextQuery).getResultList();
15:04:29,704 DEBUG SQL:92 – select this_.id as id1_0_0_, this_.message as message2_0_0_, this_.postedAt as postedAt3_0_0_, this_.url as url4_0_0_, this_.userName as userName5_0_0_, this_.version as version6_0_0_ from Tweet this_ where (this_.id in (?, ?))
15:04:29,707 INFO TestSearchTweets:55 – Tweet [id=3, postedAt=2017-02-02 00:00:00.0, userName=thjanssen123, message=How to automatically validate entities with Hibernate Validator BeanValidation, url=http://www.thoughts-on-java.org/automatically-validate-entities-with-hibernate-validator/, version=0]
15:04:29,707 INFO TestSearchTweets:55 – Tweet [id=2, postedAt=2017-01-24 00:00:00.0, userName=thjanssen123, message=5 tips to write efficient queries with JPA and Hibernate, url=http://www.thoughts-on-java.org/5-tips-write-efficient-queries-jpa-hibernate/, version=0]1
EntityManager em = emf.createEntityManager();
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
fullTextEntityManager.createIndexer().startAndWait();
@Indexed
@Entity
public class Tweet {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = “id”, updatable = false, nullable = false)
private Long id;
@Column
private Date postedAt;
@Column
@Field
private String userName;
@Column
@Field
private String message;
@Column
private String url;
@Version
private Long version;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment