本文整理汇总了Java中javax.persistence.EntityGraph.addAttributeNodes方法的典型用法代码示例。如果您正苦于以下问题:Java EntityGraph.addAttributeNodes方法的具体用法?Java EntityGraph.addAttributeNodes怎么用?Java EntityGraph.addAttributeNodes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.persistence.EntityGraph
的用法示例。
在下文中一共展示了EntityGraph.addAttributeNodes方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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);
}
示例2: 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;
}
示例3: 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.");
}
示例4: testInitDynamicEntityGraph
import javax.persistence.EntityGraph; //导入方法依赖的package包/类
@Test
public void testInitDynamicEntityGraph() {
EntityGraph<User> graph = entityManager.createEntityGraph(User.class);
graph.addAttributeNodes("cars");
User user = entityManager.find(User.class, 1L, Collections.singletonMap("javax.persistence.fetchgraph", graph));
user.getCars().stream().forEach(System.out::println);
}
示例5: shouldLazyInitializationSolution5
import javax.persistence.EntityGraph; //导入方法依赖的package包/类
@Test
public void shouldLazyInitializationSolution5() { //encji
Map<String, Object> hints = newHashMap();
EntityGraph<Item> itemGraph = em.createEntityGraph(Item.class); //<13>
itemGraph.addAttributeNodes("offers"); //<14>
hints.put("javax.persistence.loadgraph", itemGraph); //<15>
em.getEntityGraphs(Item.class).forEach(eg->log.info("{}",eg));
Item one = em.find(Item.class, 1l,hints);
assertThat(Persistence.getPersistenceUtil().isLoaded(one)).isTrue();
assertThat(Persistence.getPersistenceUtil().isLoaded(one.getOffers())).isTrue();
}
示例6: shouldEntityGraphWork
import javax.persistence.EntityGraph; //导入方法依赖的package包/类
@Test
public void shouldEntityGraphWork() {
Map<String, Object> props = newHashMap();
EntityGraph<Skill> skillGraph = em.createEntityGraph(Skill.class);
skillGraph.addAttributeNodes("candidate");
props.put("javax.persistence.loadgraph", skillGraph);
Skill one = em.find(Skill.class, 1l, props);
assertThat(Persistence.getPersistenceUtil().isLoaded(one)).isTrue();
assertThat(Persistence.getPersistenceUtil().isLoaded(one.getCandidate())).isTrue();
}
示例7: generateEntityGraphXPath
import javax.persistence.EntityGraph; //导入方法依赖的package包/类
/**
* Builds an entity graph from a XML file.
*
* @param graphName
* name of the entity graph
* @param classType
* entity graph with rootElement of classType
* @return result generated entity graph with rootElement of classType - can
* be null if an error occurs during entity graph initialization
*/
@Override
public <T> EntityGraph<?> generateEntityGraphXPath(final EntityManager em, final String graphName,
final Class<T> classType) {
EntityGraph<?> graph = null;
try {
URL centralMappingFilePath = JEntityGraphDataMapper.class.getResource(FilePaths.CENTRAL_XML_NAME.path());
File centralRules;
centralRules = new File(centralMappingFilePath.toURI());
final DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
final Document doc = dBuilder.parse(centralRules);
doc.getDocumentElement().normalize();
xPath = XPathFactory.newInstance().newXPath();
// list of attribute-nodes from the root class of the selected
// entity graph
final String attributeRootNodeListString = "//named-entity-graph[@name='" + graphName
+ "']/named-attribute-node";
final NodeList attributeRootNodeList = (NodeList) xPath.compile(attributeRootNodeListString).evaluate(doc,
XPathConstants.NODESET);
// list of all subgraphs
final String subgraphListString = "//named-entity-graph[@name='" + graphName + "']/subgraph";
final NodeList subgraphList = (NodeList) xPath.compile(subgraphListString).evaluate(doc,
XPathConstants.NODESET);
// create EntityGraph
graph = em.createEntityGraph(classType);
// reads all named-attribute-nodes below the named-entity-graph
for (int i = 0; i < attributeRootNodeList.getLength(); ++i) {
final Element nodeElem = (Element) attributeRootNodeList.item(i);
// adds the attributes to the graph
if ("".equals(nodeElem.getAttribute("subgraph"))) {
graph.addAttributeNodes(nodeElem.getAttribute("name"));
}
// reference to a subgraph
else {
// create subgraph with a reference to the upper class
final Subgraph<?> subgraph = graph.addSubgraph(nodeElem.getAttribute("name"));
buildSubgraph(subgraph, graphName, nodeElem, subgraphList, doc);
}
}
} catch (XPathExpressionException | ParserConfigurationException | SAXException | IOException
| URISyntaxException e) {
LOGGER.warn("Exception in BuildEntityGraph::generateEntityGraphXPath, full stack trace follows: ", e);
}
return graph;
}