java - Hibernate & JPA 2.1, How to retrieve the name of a subgraph from entity graph -
i try name of subgraph (defined in annotation of entity class), hibernate implementation subgraphimpl has no getter name.
is there other possibility name of subgraph programmatically?
there class called namedsubgraph, how namedsupgraph subgraphimpl?
what try develop entity graph validator, checks using reflection if needed attributes nodes defined (checking fields of entity class reflection). possible exclude , include attribute nodes partial entity graphs.
but need subgraph name identify correct subgraphs in entity graph should validated.
example:
a working example available on github account
four entity classes: person, address creditcard , debitcard
person.java:
@entity @namedentitygraphs({ @namedentitygraph( name = person.eg_profile_full, attributenodes = { @namedattributenode(value = "wallet", subgraph = wallet.eg_profile_full) } ), @namedentitygraph( name = person.eg_profile_partial, attributenodes = { @namedattributenode(value = "wallet", subgraph = wallet.eg_profile_partial) } ) }) public class person { @transient public transient static final string eg_profile_full = "personfull"; @transient public transient static final string eg_profile_partial = "personcreditcards"; @id long id; @joincolumn(name = "wallet_id") @onetoone(cascade = cascadetype.all) wallet wallet; }
wallet.java:
@entity @namedentitygraphs({ @namedentitygraph( name = wallet.eg_profile_full, attributenodes = { @namedattributenode(value = "creditcards", subgraph = creditcard.eg_profile_full), @namedattributenode(value = "debitcards", subgraph = debitcard.eg_profile_full) } ), @namedentitygraph( name = wallet.eg_profile_partial, attributenodes = { @namedattributenode(value = "creditcards", subgraph = creditcard.eg_profile_full) } ) }) public class wallet { @transient public transient static final string eg_profile_full = "walletfull"; @transient public transient static final string eg_profile_partial = "walletcredit"; @id long id; @onetomany(cascade = cascadetype.all) @ordercolumn(name = "idx") @joincolumn(name = "wallet_id") list<creditcard> creditcards; @onetomany(cascade = cascadetype.all) @ordercolumn(name = "idx") @joincolumn(name = "wallet_id") list<debitcard> debitcards; }
creditcard.java:
@entity @namedentitygraph( name = creditcard.eg_profile_full, attributenodes = { @namedattributenode(value = "number"), @namedattributenode(value = "name") } ) public class creditcard { @transient public transient static final string eg_profile_full = "creditcardfull"; @id long id; @column(name="creditcard_number") int number; @column(name="creditcard_name") string name; }
debitcard.java:
@entity @namedentitygraph( name = debitcard.eg_profile_full, attributenodes = { @namedattributenode(value = "key") } ) public class debitcard { @transient public transient static final string eg_profile_full = "debitcardfull"; @id long id; @column(name="debitcard_key") long key; }
i wrote class print entity graphs entitygraphexamplemain.java:
public class entitygraphexamplemain { protected static final string context_location = "/inmemory-database-test-annotation-context.xml"; public static void main(string[] args) { // entity graph name. classpathxmlapplicationcontext context = new classpathxmlapplicationcontext(context_location); entitymanager em = ((entitymanagerfactory) context.getbean("entitymanagerfactory")).createentitymanager(); entitygraphimpl<?> entitygraph = (entitygraphimpl<?>) em.getentitygraph(person.eg_profile_full); // print entity graph name system.out.println("entitygraph = '" + entitygraph.getname() + "'"); // print entity graph attribute node list size system.out.println("entitygraph.attributenodes.size = '" + entitygraph.getattributenodes().size() + "'"); // wallet attribute node. attributenode<?> attributenode = entitygraph.getattributenodes().get(0); // convert , receive needed objects attributenodeimpl hbattributenode = (attributenodeimpl) attributenode; attribute jpaattribute = hbattributenode.getattribute(); // print attribute node name system.out.println("attributenode = '" + attributenode.getattributename() + "'"); // subgraph of wallet map<class, subgraph> subgraphs = hbattributenode.getsubgraphs(); // subgraph map should have 1 entry system.out.println("subgraphs.size = '" + subgraphs.entryset().size() + "'"); // subgraph map should contain wallet class key. system.out.println("subgraphs.keys = '" + subgraphs.keyset() + "'"); subgraph subgraph = subgraphs.get(subgraphs.keyset().iterator().next()); subgraphimpl hbsubgraph = (subgraphimpl) subgraph; // print subgraph type system.out.println("subgraph.type = '" + subgraph.getclasstype().getname() + "'"); // try subgraph attribute nodes // subgraph attribute node list should contian 1 or 2 elements...but zero! system.out.println("(sub)attributenode.size = '" + subgraph.getattributenodes().size() + "'"); /* + ############################################################################################### + # # + # need way identify name of subgraph, entity graph using entity # + # manager. # + # # + # needed this: # + # # + # string subgraphname = subgraph.getname() # + # # + # entitygraphimpl<?> entitygraph = (entitygraphimpl<?>) em.getentitygraph(subgraphname); # + # # + ############################################################################################### + */ } }
console output:
entitygraph = 'personfull' entitygraph.attributenodes.size = '1' attributenode = 'wallet' subgraphs.size = '1' subgraphs.keys = '[class de.hbmexample1.entity.wallet]' subgraph.type = 'de.hbmexample1.entity.wallet' (sub)attributenode.size = '0'
size of attribute nodes in subgraph 0, not expected. need name of subgraph workaround mentioned in code.
/* + ############################################################################################### + # # + # need way identify name of subgraph, entity graph using entity # + # manager. # + # # + # needed this: # + # # + # string subgraphname = subgraph.getname() # + # # + # entitygraphimpl<?> entitygraph = (entitygraphimpl<?>) em.getentitygraph(subgraphname); # + # # + ############################################################################################### + */
Comments
Post a Comment