本文整理汇总了Java中javax.persistence.EntityGraph类的典型用法代码示例。如果您正苦于以下问题:Java EntityGraph类的具体用法?Java EntityGraph怎么用?Java EntityGraph使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
EntityGraph类属于javax.persistence包,在下文中一共展示了EntityGraph类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadFully
import javax.persistence.EntityGraph; //导入依赖的package包/类
/**
* Fetches an instance of given PhoneBookEntry with all lazy
* loaded properties loaded.
* @param entry
* @return the fully loaded instance
*/
public PhoneBookEntry loadFully(PhoneBookEntry entry) {
// To get lazy loaded fields initialized, you have couple of options,
// all with pros and cons, 3 of them presented here.
// 1) use an explicit join query (with EntityManager or @Query annotation
// in repository method.
// em.createQuery("select e from PhoneBookEntry e LEFT JOIN FETCH e.groups where e.id = :id", PhoneBookEntry.class);
// ...
// 2) use EntityGraph's introduced in JPA 2.1, here constructed dynamically
// and passed via QueryResult object from DeltaSpike Data. You can
// also use entity graphs with @Query annotation in repositories or
// with raw EntityManager API.
EntityGraph<PhoneBookEntry> graph = this.em.createEntityGraph(
PhoneBookEntry.class);
graph.addSubgraph("groups");
entry = entryRepo.findById(entry.getId())
.hint("javax.persistence.loadgraph", graph)
.getSingleResult();
// 3) ..or use the infamous size() hack that all of us actually do :-)
entry.getAddresses().size();
return entry;
}
示例2: addAttributeNodes
import javax.persistence.EntityGraph; //导入依赖的package包/类
private static void addAttributeNodes(String fieldName, EntityGraph<?> graph) {
int pos = fieldName.indexOf(GRAPH_DELIMETER);
if (pos < 0) {
graph.addAttributeNodes(fieldName);
return;
}
String subgraphName = fieldName.substring(0, pos);
Subgraph<?> subGraph = graph.addSubgraph(subgraphName);
String nextFieldName = fieldName.substring(pos + 1);
pos = nextFieldName.indexOf(GRAPH_DELIMETER);
while (pos > 0) {
subgraphName = nextFieldName.substring(0, pos);
subGraph = graph.addSubgraph(subgraphName);
nextFieldName = nextFieldName.substring(pos + 1);
pos = nextFieldName.indexOf(GRAPH_DELIMETER);
}
subGraph.addAttributeNodes(nextFieldName);
}
示例3: findAllWithAttributes
import javax.persistence.EntityGraph; //导入依赖的package包/类
@Override
public List<E> findAllWithAttributes(String... attributes) {
// String sql = "SELECT o FROM " + getTableName() + " o";
EntityGraph<E> graph = entityManager.createEntityGraph(getEntityClass());
graph.addAttributeNodes(attributes);
// TypedQuery<E> query = entityManager.createQuery(sql, getEntityClass());
// query.setHint("javax.persistence.loadgraph", graph);
// TypedQuery<E> query = entityManager.createQuery(sql, getEntityClass());
// LoadGroup lg = new LoadGroup();
// lg.addAttributes(Arrays.asList(attributes));
// query.setHint(QueryHints.LOAD_GROUP, lg);
// for (String att : attributes) {
// query.setHint(QueryHints.BATCH, "o." + att);
// }
// List<E> result = query.getResultList();
List<E> result = findAllWithGraph(graph);
return result;
}
示例4: selectWithEntityGraph
import javax.persistence.EntityGraph; //导入依赖的package包/类
@SuppressWarnings("unchecked")
@Test
public void selectWithEntityGraph() {
log.info("... selectWithEntityGraph ...");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
EntityGraph<Author> graph = em.createEntityGraph(Author.class);
graph.addAttributeNodes(Author_.books);
TypedQuery<Author> q = em.createQuery("SELECT a FROM Author a WHERE a.id = 1", Author.class);
q.setHint("javax.persistence.fetchgraph", graph);
Author a = q.getSingleResult();
em.getTransaction().commit();
em.close();
log.info(a.getFirstName()+" "+a.getLastName()+" wrote "+a.getBooks().size()+" books.");
}
示例5: selectWithNamedEntityGraph
import javax.persistence.EntityGraph; //导入依赖的package包/类
@Test
public void selectWithNamedEntityGraph() {
log.info("... selectWithNamedEntityGraph ...");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
EntityGraph<?> graph = em.createEntityGraph("graph.AuthorBooks");
TypedQuery<Author> q = em.createQuery("SELECT a FROM Author a WHERE a.id = 1", Author.class);
q.setHint("javax.persistence.fetchgraph", graph);
Author a = q.getSingleResult();
em.getTransaction().commit();
em.close();
log.info(a.getFirstName()+" "+a.getLastName()+" wrote "+a.getBooks().size()+" books.");
}
示例6: getMinimalRouteHeader
import javax.persistence.EntityGraph; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public DocumentRouteHeaderValue getMinimalRouteHeader(String documentId) {
// This graph is defined on the DocumentRouteHeaderValue class.
EntityGraph<DocumentRouteHeaderValue> entityGraph =
(EntityGraph<DocumentRouteHeaderValue>) entityManager.createEntityGraph("DocumentRouteHeaderValue.ActionListAttributesOnly");
TypedQuery<DocumentRouteHeaderValue> query = entityManager.createQuery("SELECT rh FROM DocumentRouteHeaderValue rh WHERE rh.documentId = :documentId", DocumentRouteHeaderValue.class );
// By using the graph - all properties but those on the graph should have
// a lazy proxy in place. Attempting to access any of those *should* cause the
// rest of the properties to load.
query.setHint("javax.persistence.fetchgraph", entityGraph);
query.setParameter("documentId", documentId);
List<DocumentRouteHeaderValue> result = query.getResultList();
if ( result.isEmpty() ) {
return null;
}
return result.get(0);
}
示例7: findKundenByNachname
import javax.persistence.EntityGraph; //导入依赖的package包/类
/**
* Suche nach Kunden �ber Nachnamen
* @param name Der angegebene Nachname
* @return Liste der Kunden, die den angegebenen Nachnamen besitzen
*/
@NotNull(message = "{kunde.notFound.name}")
public List<Kunde> findKundenByNachname(String name, FetchType fetchType) {
final TypedQuery<Kunde> query = em.createNamedQuery(Kunde.FIND_KUNDEN_BY_NACHNAME, Kunde.class)
.setParameter(Kunde.PARAM_KUNDE_NACHNAME, name);
EntityGraph<?> entityGraph;
switch(fetchType) {
case NUR_KUNDE:
break;
case MIT_BESTELLUNGEN:
entityGraph = em.getEntityGraph(Kunde.GRAPH_BESTELLUNGEN);
query.setHint(LOADGRAPH, entityGraph);
break;
default:
break;
}
return query.getResultList();
}
示例8: findAllKunden
import javax.persistence.EntityGraph; //导入依赖的package包/类
/**
* Suche nach Liste aller Kunden
* @return Gibt alle vorhandenen Kunden zur�ck
*/
public List<Kunde> findAllKunden(FetchType fetchType, OrderType orderType) {
final TypedQuery<Kunde> query = OrderType.ID.equals(orderType)
? em.createNamedQuery(Kunde.FIND_KUNDEN_ORDER_BY_ID, Kunde.class)
: em.createNamedQuery(Kunde.FIND_KUNDEN, Kunde.class);
EntityGraph<?> entityGraph;
switch(fetchType) {
case NUR_KUNDE:
break;
case MIT_BESTELLUNGEN:
entityGraph = em.getEntityGraph(Kunde.GRAPH_BESTELLUNGEN);
query.setHint(LOADGRAPH, entityGraph);
break;
default:
break;
}
return query.getResultList();
}
示例9: getDefaultGraph
import javax.persistence.EntityGraph; //导入依赖的package包/类
/**
* Build or return the default Entity Graph that represents defaultExpand
*
* @param session
*
* @return
*/
public EntityGraph getDefaultGraph(final Session session)
{
if (this.defaultExpandGraph == null)
{
final EntityGraph<?> graph = session.createEntityGraph(clazz);
populateGraph(graph, getEagerFetch());
this.defaultExpandGraph = graph;
return graph;
}
else
{
return this.defaultExpandGraph;
}
}
示例10: init
import javax.persistence.EntityGraph; //导入依赖的package包/类
public void init() {
log.info("call init, id @" + id);
if (id != null) {
//this.post = em.find(Post.class, this.id);
EntityGraph postEntityGraph=em.getEntityGraph("post");
// EntityGraph postEntityGraph=em.createEntityGraph(Post.class);
// postEntityGraph.addAttributeNodes("title");
// postEntityGraph.addSubgraph("comments").addAttributeNodes("content");
this.post=em
.createQuery("select p from Post p where p.id=:id", Post.class)
.setHint("javax.persistence.loadgraph", postEntityGraph)
.setParameter("id", this.id)
.getResultList()
.get(0);
PersistenceUnitUtil util=em.getEntityManagerFactory().getPersistenceUnitUtil();
log.info("title is [email protected]"+util.isLoaded(this.post, "title"));
log.info("body is [email protected]"+util.isLoaded(this.post, "body"));
log.info("comments is [email protected]"+util.isLoaded(this.post, "comments"));
} else {
throw new RuntimeException("id is required");
}
}
示例11: shouldLoadArtistWithLiveConcertsAndContracts
import javax.persistence.EntityGraph; //导入依赖的package包/类
@Test
@InSequence(5)
public void shouldLoadArtistWithLiveConcertsAndContracts()
throws Exception{
EntityGraph artistGraph = em.getEntityGraph("Artist.WithConcertsAndContracts");
PersistenceUnitUtil util =
em.getEntityManagerFactory().getPersistenceUnitUtil();
Artist artist = getArtistWithEntityGraph(
"Artist.WithConcertsAndContracts");
System.out.printf("++++ artist=%s\n", artist);
assertTrue(util.isLoaded(artist, "id"));
assertTrue(util.isLoaded(artist, "name"));
assertTrue(util.isLoaded(artist, "events"));
ConcertEvent event = artist.getEvents().values().iterator().next();
System.out.printf("++++ concert event=%s\n", event );
assertTrue(util.isLoaded(event, "id"));
assertTrue(util.isLoaded(event, "name"));
assertTrue(util.isLoaded(event, "eventType"));
assertTrue(util.isLoaded(event, "contracts"));
}
示例12: shouldLoadArtistWithoutConcerts
import javax.persistence.EntityGraph; //导入依赖的package包/类
@Test
@InSequence(2)
public void shouldLoadArtistWithoutConcerts() throws Exception{
PersistenceUnitUtil util =
em.getEntityManagerFactory().getPersistenceUnitUtil();
EntityGraph artistGraph = em.getEntityGraph("Artist.NoConcerts");
Artist artist = (Artist) em.createQuery("Select a from Artist a")
.setHint("javax.persistence.fetchgraph", artistGraph)
.getResultList()
.get(0);
System.out.printf("++++ artist=%s\n", artist);
System.out.printf(">> loaded artist.id = %s\n", util.isLoaded(artist, "id"));
System.out.printf(">> loaded artist.name = %s\n", util.isLoaded(artist, "name"));
System.out.printf(">> loaded artist.events = %s\n", util.isLoaded(artist, "events"));
assertTrue(util.isLoaded(artist, "id"));
assertTrue(util.isLoaded(artist, "name"));
assertFalse(util.isLoaded(artist, "events"));
}
示例13: shouldLoadArtistWithConcerts
import javax.persistence.EntityGraph; //导入依赖的package包/类
@Test
@InSequence(3)
public void shouldLoadArtistWithConcerts() throws Exception{
PersistenceUnitUtil util =
em.getEntityManagerFactory().getPersistenceUnitUtil();
EntityGraph artistGraph = em.getEntityGraph("Artist.WithConcerts");
Artist artist = (Artist)
em.createQuery("Select a from Artist a")
.setHint("javax.persistence.fetchgraph", artistGraph)
.getResultList()
.get(0);
System.out.printf("++++ artist=%s\n", artist);
System.out.printf("artist=%s\n", artist);
assertTrue(util.isLoaded(artist, "id"));
assertTrue(util.isLoaded(artist, "name"));
assertTrue(util.isLoaded(artist, "events"));
}
示例14: shouldLoadArtistWithLiveConcertsAndNoContracts
import javax.persistence.EntityGraph; //导入依赖的package包/类
@Test
@InSequence(4)
public void shouldLoadArtistWithLiveConcertsAndNoContracts()
throws Exception {
PersistenceUnitUtil util =
em.getEntityManagerFactory().getPersistenceUnitUtil();
em.clear();
EntityGraph artistGraph = em.getEntityGraph(
"Artist.WithConcertsAndNoContracts");
Artist artist = (Artist)
em.createQuery("Select a from Artist a")
.setHint("javax.persistence.fetchgraph", artistGraph)
.getResultList()
.get(0);
System.out.printf("++++ artist=%s\n", artist);
assertTrue(util.isLoaded(artist, "id"));
assertTrue(util.isLoaded(artist, "name"));
assertTrue(util.isLoaded(artist, "events"));
ConcertEvent event = artist.getEvents().values().iterator().next();
System.out.printf("++++ concert event=%s\n", event );
assertTrue(util.isLoaded(event, "id"));
assertTrue(util.isLoaded(event, "name"));
assertTrue(util.isLoaded(event, "eventType"));
assertFalse(util.isLoaded(event, "contracts"));
}
示例15: shouldLoadArtistWithLiveConcertsAndContracts
import javax.persistence.EntityGraph; //导入依赖的package包/类
@Test
@InSequence(5)
public void shouldLoadArtistWithLiveConcertsAndContracts()
throws Exception{
PersistenceUnitUtil util =
em.getEntityManagerFactory().getPersistenceUnitUtil();
em.clear();
EntityGraph artistGraph = em.getEntityGraph("Artist.WithConcertsAndContracts");
Artist artist = (Artist)
em.createQuery("Select a from Artist a")
.setHint("javax.persistence.fetchgraph", artistGraph)
.getResultList()
.get(0);
System.out.printf("++++ artist=%s\n", artist);
assertTrue(util.isLoaded(artist, "id"));
assertTrue(util.isLoaded(artist, "name"));
assertTrue(util.isLoaded(artist, "events"));
ConcertEvent event = artist.getEvents().values().iterator().next();
System.out.printf("++++ concert event=%s\n", event );
assertTrue(util.isLoaded(event, "id"));
assertTrue(util.isLoaded(event, "name"));
assertTrue(util.isLoaded(event, "eventType"));
assertTrue(util.isLoaded(event, "contracts"));
}