当前位置: 首页>>代码示例>>Java>>正文


Java EntityGraph类代码示例

本文整理汇总了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;
}
 
开发者ID:mstahv,项目名称:jpa-addressbook,代码行数:31,代码来源:PhoneBookService.java

示例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);
}
 
开发者ID:xm-online,项目名称:xm-ms-entity,代码行数:22,代码来源:EntityGraphRepositoryImpl.java

示例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;
}
 
开发者ID:viydaag,项目名称:dungeonstory-java,代码行数:23,代码来源:AbstractRepository.java

示例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.");
}
 
开发者ID:thjanssen,项目名称:HibernateTips,代码行数:21,代码来源:TestEntityGraph.java

示例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.");
}
 
开发者ID:thjanssen,项目名称:HibernateTips,代码行数:18,代码来源:TestEntityGraph.java

示例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);
}
 
开发者ID:kuali,项目名称:kc-rice,代码行数:21,代码来源:ActionListDAOJpaImpl.java

示例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();
}
 
开发者ID:dastdev,项目名称:shop-repo,代码行数:26,代码来源:KundeService.java

示例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();
}
 
开发者ID:dastdev,项目名称:shop-repo,代码行数:25,代码来源:KundeService.java

示例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;
	}
}
 
开发者ID:petergeneric,项目名称:stdlib,代码行数:25,代码来源:QEntity.java

示例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");
        }
    }
 
开发者ID:hantsy,项目名称:ee7-sandbox,代码行数:26,代码来源:ViewPostBean.java

示例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"));
}
 
开发者ID:peterpilgrim,项目名称:javaee7-developer-handbook,代码行数:21,代码来源:ArtistEntityGraphRetrievalTest.java

示例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"));
}
 
开发者ID:peterpilgrim,项目名称:javaee7-developer-handbook,代码行数:19,代码来源:ArtistEntityGraphRetrievalTest.java

示例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"));
}
 
开发者ID:peterpilgrim,项目名称:javaee7-developer-handbook,代码行数:18,代码来源:ArtistEntityGraphRetrievalTest.java

示例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"));
}
 
开发者ID:peterpilgrim,项目名称:javaee7-developer-handbook,代码行数:26,代码来源:ArtistEntityGraphRetrievalTest.java

示例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"));
}
 
开发者ID:peterpilgrim,项目名称:javaee7-developer-handbook,代码行数:25,代码来源:ArtistEntityGraphRetrievalTest.java


注:本文中的javax.persistence.EntityGraph类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。