Advertisement
Guest User

TestEntity.java

a guest
Mar 4th, 2021
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.47 KB | None | 0 0
  1. import java.lang.reflect.Field;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Comparator;
  5. import java.util.TreeMap;
  6. import java.util.stream.Collectors;
  7. import javax.persistence.Column;
  8. import javax.persistence.FetchType;
  9. import javax.persistence.GeneratedValue;
  10. import javax.persistence.Id;
  11. import javax.persistence.JoinColumn;
  12. import javax.persistence.ManyToOne;
  13. import javax.persistence.Table;
  14. import javax.persistence.TypedQuery;
  15. import javax.persistence.Version;
  16. import javax.persistence.criteria.CriteriaBuilder;
  17. import javax.persistence.criteria.CriteriaQuery;
  18. import javax.persistence.criteria.Path;
  19. import javax.persistence.criteria.Root;
  20. import org.hibernate.Hibernate;
  21. import org.hibernate.Session;
  22. import org.hibernate.annotations.Fetch;
  23. import org.hibernate.annotations.FetchMode;
  24. import org.hibernate.annotations.GenericGenerator;
  25. import org.hibernate.annotations.LazyToOne;
  26. import org.hibernate.annotations.LazyToOneOption;
  27. import org.hibernate.annotations.NotFound;
  28. import org.hibernate.annotations.NotFoundAction;
  29. import org.hibernate.annotations.OptimisticLock;
  30. import org.hibernate.annotations.Parameter;
  31. import org.hibernate.graph.GraphParser;
  32. import org.hibernate.graph.RootGraph;
  33.  
  34. @javax.persistence.Entity
  35. @Table(name = "loading_test")
  36. @org.hibernate.annotations.BatchSize(size = 100)
  37. @org.hibernate.annotations.DynamicInsert(false)
  38. @org.hibernate.annotations.DynamicUpdate(false)
  39. @org.hibernate.annotations.SelectBeforeUpdate(false)
  40. @org.hibernate.annotations.Proxy(lazy = false)
  41. @org.hibernate.annotations.OptimisticLocking(type = org.hibernate.annotations.OptimisticLockType.VERSION)
  42. @org.hibernate.annotations.Polymorphism(type = org.hibernate.annotations.PolymorphismType.IMPLICIT)
  43. @javax.persistence.Access(javax.persistence.AccessType.FIELD)
  44. public class TestEntity {
  45.     @Id
  46.     @Column(name = "id", nullable = false, updatable = false)
  47.     @org.hibernate.annotations.Type(type = "long")
  48.     private Long id = null;
  49.  
  50.     @Version
  51.     @Column(name = "version", nullable = false)
  52.     @org.hibernate.annotations.Type(type = "int")
  53.     private int version = 0;
  54.  
  55.     @Column(name = "name")
  56.     @javax.persistence.Basic
  57.     @org.hibernate.annotations.OptimisticLock(excluded = false)
  58.     @org.hibernate.annotations.Type(type = "java.lang.String")
  59.     private String name;
  60.  
  61.     @ManyToOne
  62.     @JoinColumn(name = "link_id", nullable = true)
  63.     @OptimisticLock(excluded = false)
  64.     @NotFound(action = NotFoundAction.EXCEPTION)
  65.     public TestEntity link;
  66.  
  67.     @ManyToOne
  68.     @Fetch(FetchMode.SELECT)
  69.     @JoinColumn(name = "linkSelect_id", nullable = true)
  70.     @OptimisticLock(excluded = false)
  71.     @NotFound(action = NotFoundAction.EXCEPTION)
  72.     public TestEntity linkSelect;
  73.  
  74.     @ManyToOne
  75.     @Fetch(FetchMode.JOIN)
  76.     @JoinColumn(name = "linkJoin_id", nullable = true)
  77.     @OptimisticLock(excluded = false)
  78.     @NotFound(action = NotFoundAction.EXCEPTION)
  79.     public TestEntity linkJoin;
  80.  
  81.     @ManyToOne
  82.     @LazyToOne(LazyToOneOption.NO_PROXY)
  83.     @JoinColumn(name = "linkNoProxy_id", nullable = true)
  84.     @OptimisticLock(excluded = false)
  85.     @NotFound(action = NotFoundAction.EXCEPTION)
  86.     public TestEntity linkNoProxy;
  87.  
  88.     @ManyToOne
  89.     @LazyToOne(LazyToOneOption.NO_PROXY)
  90.     @Fetch(FetchMode.SELECT)
  91.     @JoinColumn(name = "linkNoProxySelect_id", nullable = true)
  92.     @OptimisticLock(excluded = false)
  93.     @NotFound(action = NotFoundAction.EXCEPTION)
  94.     public TestEntity linkNoProxySelect;
  95.  
  96.     @ManyToOne
  97.     @LazyToOne(LazyToOneOption.NO_PROXY)
  98.     @Fetch(FetchMode.JOIN)
  99.     @JoinColumn(name = "linkNoProxyJoin_id", nullable = true)
  100.     @OptimisticLock(excluded = false)
  101.     @NotFound(action = NotFoundAction.EXCEPTION)
  102.     public TestEntity linkNoProxyJoin;
  103.  
  104.     @ManyToOne(fetch = FetchType.LAZY)
  105.     @JoinColumn(name = "linkLazy_id", nullable = true)
  106.     @OptimisticLock(excluded = false)
  107.     @NotFound(action = NotFoundAction.EXCEPTION)
  108.     public TestEntity linkLazy;
  109.  
  110.     @ManyToOne(fetch = FetchType.LAZY)
  111.     @Fetch(FetchMode.SELECT)
  112.     @JoinColumn(name = "linkLazySelect_id", nullable = true)
  113.     @OptimisticLock(excluded = false)
  114.     @NotFound(action = NotFoundAction.EXCEPTION)
  115.     public TestEntity linkLazySelect;
  116.  
  117.     @ManyToOne(fetch = FetchType.LAZY)
  118.     @Fetch(FetchMode.JOIN)
  119.     @JoinColumn(name = "linkLazyJoin_id", nullable = true)
  120.     @OptimisticLock(excluded = false)
  121.     @NotFound(action = NotFoundAction.EXCEPTION)
  122.     public TestEntity linkLazyJoin;
  123.  
  124.     @ManyToOne(fetch = FetchType.LAZY)
  125.     @LazyToOne(LazyToOneOption.NO_PROXY)
  126.     @JoinColumn(name = "linkLazyNoProxy_id", nullable = true)
  127.     @OptimisticLock(excluded = false)
  128.     @NotFound(action = NotFoundAction.EXCEPTION)
  129.     public TestEntity linkLazyNoProxy;
  130.  
  131.     @ManyToOne(fetch = FetchType.LAZY)
  132.     @LazyToOne(LazyToOneOption.NO_PROXY)
  133.     @Fetch(FetchMode.SELECT)
  134.     @JoinColumn(name = "linkLazyNoProxySelect_id", nullable = true)
  135.     @OptimisticLock(excluded = false)
  136.     @NotFound(action = NotFoundAction.EXCEPTION)
  137.     public TestEntity linkLazyNoProxySelect;
  138.  
  139.     @ManyToOne(fetch = FetchType.LAZY)
  140.     @LazyToOne(LazyToOneOption.NO_PROXY)
  141.     @Fetch(FetchMode.JOIN)
  142.     @JoinColumn(name = "linkLazyNoProxyJoin_id", nullable = true)
  143.     @OptimisticLock(excluded = false)
  144.     @NotFound(action = NotFoundAction.EXCEPTION)
  145.     public TestEntity linkLazyNoProxyJoin;
  146.  
  147.     // Test and results data
  148.     private static final ArrayList<Field> fields = new ArrayList<>(
  149.         Arrays.stream(TestEntity.class.getDeclaredFields())
  150.             .filter(f -> f.getName().startsWith("link"))
  151.             .sorted(Comparator.comparing(Field::getName))
  152.             .collect(Collectors.toList()));
  153.  
  154.     private static TreeMap<String, Integer> initializedCounts = new TreeMap<>();
  155.     private static TreeMap<String, Integer> reflectionAccessCounts = new TreeMap<>();
  156.     private static int runCount = 0;
  157.  
  158.     public Long getId() {
  159.         return id;
  160.     }
  161.  
  162.     public int getVersion() {
  163.         return version;
  164.     }
  165.  
  166.     public String getName() {
  167.         return name;
  168.     }
  169.  
  170.     public vid setName(final String name) {
  171.         this.name = name;
  172.     }
  173.  
  174.     public TestEntity getLink() {
  175.         return link;
  176.     }
  177.  
  178.     public vid setLink(final TestEntity link) {
  179.         this.link = link;
  180.     }
  181.  
  182.     public TestEntity getLinkSelect() {
  183.         return linkSelect;
  184.     }
  185.  
  186.     public vid setLinkSelect(final TestEntity linkSelect) {
  187.         this.linkSelect = linkSelect;
  188.     }
  189.  
  190.     public TestEntity getLinkJoin() {
  191.         return linkJoin;
  192.     }
  193.  
  194.     public vid setLinkJoin(final TestEntity linkJoin) {
  195.         this.linkJoin = linkJoin;
  196.     }
  197.  
  198.     public TestEntity getLinkNoProxy() {
  199.         return linkNoProxy;
  200.     }
  201.  
  202.     public vid setLinkNoProxy(final TestEntity linkNoProxy) {
  203.         this.linkNoProxy = linkNoProxy;
  204.     }
  205.  
  206.     public TestEntity getLinkNoProxySelect() {
  207.         return linkNoProxySelect;
  208.     }
  209.  
  210.     public vid setLinkNoProxySelect(final TestEntity linkNoProxySelect) {
  211.         this.linkNoProxySelect = linkNoProxySelect;
  212.     }
  213.  
  214.     public TestEntity getLinkNoProxyJoin() {
  215.         return linkNoProxyJoin;
  216.     }
  217.  
  218.     public vid setLinkNoProxyJoin(final TestEntity linkNoProxyJoin) {
  219.         this.linkNoProxyJoin = linkNoProxyJoin;
  220.     }
  221.  
  222.     public TestEntity getLinkLazy() {
  223.         return linkLazy;
  224.     }
  225.  
  226.     public vid setLinkLazy(final TestEntity linkLazy) {
  227.         this.linkLazy = linkLazy;
  228.     }
  229.  
  230.     public TestEntity getLinkLazySelect() {
  231.         return linkLazySelect;
  232.     }
  233.  
  234.     public vid setLinkLazySelect(final TestEntity linkLazySelect) {
  235.         this.linkLazySelect = linkLazySelect;
  236.     }
  237.  
  238.     public TestEntity getLinkLazyJoin() {
  239.         return linkLazyJoin;
  240.     }
  241.  
  242.     public vid setLinkLazyJoin(final TestEntity linkLazyJoin) {
  243.         this.linkLazyJoin = linkLazyJoin;
  244.     }
  245.  
  246.     public TestEntity getLinkLazyNoProxy() {
  247.         return linkLazyNoProxy;
  248.     }
  249.  
  250.     public vid setLinkLazyNoProxy(final TestEntity linkLazyNoProxy) {
  251.         this.linkLazyNoProxy = linkLazyNoProxy;
  252.     }
  253.  
  254.     public TestEntity getLinkLazyNoProxySelect() {
  255.         return linkLazyNoProxySelect;
  256.     }
  257.  
  258.     public vid setLinkLazyNoProxySelect(final TestEntity linkLazyNoProxySelect) {
  259.         this.linkLazyNoProxySelect = linkLazyNoProxySelect;
  260.     }
  261.  
  262.     public TestEntity getLinkLazyNoProxyJoin() {
  263.         return linkLazyNoProxyJoin;
  264.     }
  265.  
  266.     public vid setLinkLazyNoProxyJoin(final TestEntity linkLazyNoProxyJoin) {
  267.         this.linkLazyNoProxyJoin = linkLazyNoProxyJoin;
  268.     }
  269.  
  270.     public vid reportColumns() {
  271.         for (Field field : this.getClass().getDeclaredFields()) {
  272.             System.out.print('\t');
  273.             System.out.print(field.getName());
  274.         }
  275.         System.out.println();
  276.         System.out.flush();
  277.     }
  278.  
  279.     private String pad(final Object o, int length) {
  280.         final StringBuilder b = new StringBuilder(length);
  281.         b.append(String.valueOf(o));
  282.         while (b.length() < length) b.append(' ');
  283.         return b.toString();
  284.     }
  285.  
  286.     public vid reportInitializationState() {
  287.         runCount++;
  288.         println("================================================================================");
  289.         println(toString() + " Initialization State");
  290.         println("--------------------------------------------------------------------------------");
  291.         for (Field field: fields) {
  292.             final boolean loaded = Hibernate.isPropertyInitialized(this, field.getName());
  293.             final int count;
  294.             if (loaded) {
  295.                 count = initializedCounts.computeIfAbsent(field.getName(), k -> 0) + 1;
  296.                 initializedCounts.put(field.getName(), count);
  297.             } else {
  298.                 count = initializedCounts.computeIfAbsent(field.getName(), k -> 0);
  299.             }
  300.             println(pad(field.getName() +  ":", 25) + (loaded ? "YES" : "n/a") + "    " + count + "/" + runCount);
  301.         }
  302.         println("--------------------------------------------------------------------------------");
  303.         println(toString() + " Reflection State");
  304.         println("--------------------------------------------------------------------------------");
  305.         for (Field field: fields) {
  306.             boolean loaded;
  307.             try {
  308.                 loaded = field.get(this) != null;
  309.             } catch (IllegalAccessException e) {
  310.                 e.printStackTrace();
  311.                 loaded = false;
  312.             }
  313.             final int count;
  314.             if (loaded) {
  315.                 count = reflectionAccessCounts.computeIfAbsent(field.getName(), k -> 0) + 1;
  316.                 reflectionAccessCounts.put(field.getName(), count);
  317.             } else {
  318.                 count = reflectionAccessCounts.computeIfAbsent(field.getName(), k -> 0);
  319.             }
  320.             println(pad(field.getName() +  ":", 25) + (loaded ? "HERE" : "null") + "   " + count + "/" + runCount);
  321.         }
  322.         println("================================================================================");
  323.     }
  324.    
  325.     public static vid println(Object what) {
  326.         System.err.flush();
  327.         System.out.println("######### " + String.valueOf(what));
  328.         System.out.flush();
  329.         System.err.flush();
  330.     }
  331.  
  332.  
  333.     public String toString() {
  334.         return "#" + id + ":" + name;
  335.     }
  336.  
  337.     public vid printAll() {
  338.         println("================================================================================");
  339.         println(toString());
  340.         println("--------------------------------------------------------------------------------");
  341.         println("link                     = " + link);
  342.         println("linkSelect               = " + linkSelect);
  343.         println("linkJoin                 = " + linkJoin);
  344.         println("linkNoProxy              = " + linkNoProxy);
  345.         println("linkNoProxySelect        = " + linkNoProxySelect);
  346.         println("linkNoProxyJoin          = " + linkNoProxyJoin);
  347.         println("linkLazy                 = " + linkLazy);
  348.         println("linkLazySelect           = " + linkLazySelect);
  349.         println("linkLazyJoin             = " + linkLazyJoin);
  350.         println("linkLazyNoProxy          = " + linkLazyNoProxy);
  351.         println("linkLazyNoProxySelect    = " + linkLazyNoProxySelect);
  352.         println("linkLazyNoProxyJoin      = " + linkLazyNoProxyJoin);
  353.         println("================================================================================");
  354.     }
  355.  
  356.     public static vid testLoading(final long id, int graph, int fetch, final boolean printAll, final String hintName) {
  357.         try (final Session session = getNewHibernateSession()) {
  358.             final CriteriaBuilder builder = session.getCriteriaBuilder();
  359.            
  360.             println("################################################################################");
  361.             println(" TEST " + id + (graph > 0 ? " graph=" + graph + "/" + hintName : " no graph") + (fetch > 0 ? " fetch=" + fetch : " no fetch") + (printAll ? " printAll" : ""));
  362.             println("################################################################################");
  363.             session.clear();
  364.             final CriteriaQuery<TestEntity> query = builder.createQuery(TestEntity.class);
  365.             final Root<TestEntity> root = query.from(TestEntity.class);
  366.             final Path<Long> id = root.get("id");
  367.             query.where(builder.equal(id, id));
  368.  
  369.             println("final CriteriaQuery<TestEntity> query = builder.createQuery(TestEntity.class);");
  370.             println("final Root<TestEntity> root = query.from(TestEntity.class);");
  371.             println("final Path<Long> id = root.get(\"id\");");
  372.             println("query.where(builder.equal(id, " + id + "L));");
  373.  
  374.             if (fetch > 0) {
  375.                 for (Field f: TestEntity.class.getDeclaredFields()) {
  376.                     String name = f.getName();
  377.                     if (name.startsWith("link")) {
  378.                         final javax.persistence.criteria.Fetch<TestEntity, TestEntity> linkFetch = root.fetch(name);
  379.                         println("final Fetch<TestEntity, TestEntity> " + name + "Fetch = root.fetch(\"" + name + "\");");
  380.  
  381.                         switch (fetch) {
  382.                             case 1:
  383.                                 break;
  384.  
  385.                             case 2:
  386.                                 //linkFetch.fetch("id");
  387.                                 break;
  388.  
  389.                             case 3:
  390.                                 //linkFetch.fetch("id");
  391.                                 //linkFetch.fetch("name");
  392.                                 break;
  393.  
  394.                             case 4:
  395.                                 //linkFetch.fetch("id");
  396.                                 //linkFetch.fetch("name");
  397.                                 for (Field f2: TestEntity.class.getDeclaredFields()) {
  398.                                     String name2 = f2.getName();
  399.                                     if (name2.startsWith("link")) {
  400.                                         linkFetch.fetch(name2);
  401.                                         println(name + "Fetch.fetch(\"" + name2 + "\");");
  402.                                     }
  403.                                 }
  404.                                 break;
  405.                         }
  406.                     }
  407.                 }
  408.             }
  409.  
  410.             println("final TypedQuery<TestEntity> tq = session.createQuery(query);");
  411.             final TypedQuery<TestEntity> tq = session.createQuery(query);
  412.  
  413.             if (graph > 0) {
  414.                 final StringBuilder source = new StringBuilder(1000);
  415.  
  416.                 for (Field f: TestEntity.class.getDeclaredFields()) {
  417.                     String name = f.getName();
  418.                     if (name.startsWith("link")) {
  419.                         if (source.length() > 0) {
  420.                             source.append(", ");
  421.                         }
  422.                         source.append(name);
  423.                         switch (graph) {
  424.                             case 1:
  425.                                 break;
  426.  
  427.                             case 2:
  428.                                 source.append("(id)");
  429.                                 break;
  430.  
  431.                             case 3:
  432.                                 source.append("(id, name)");
  433.                                 break;
  434.  
  435.                             case 4:
  436.                                 source.append("(id, name");
  437.                                 for (Field f2: TestEntity.class.getDeclaredFields()) {
  438.                                     String name2 = f2.getName();
  439.                                     if (name2.startsWith("link")) {
  440.                                         source.append(", ");
  441.                                         source.append(name2);
  442.                                     }
  443.                                 }
  444.                                 source.append(')');
  445.                                 break;
  446.                         }
  447.                     }
  448.                 }
  449.  
  450.                 println("final String graphText = \"" + source.toString() + "\";");
  451.                 println("final RootGraph<TestEntity> entityGraph = GraphParser.parse(TestEntity.class, graphText, session);");
  452.  
  453.                 final RootGraph<TestEntity> entityGraph =
  454.                     GraphParser.parse(TestEntity.class, source.toString(), session);
  455.  
  456.                 println("tq.setHint(\"" + hintName + "\", entityGraph);");
  457.                 tq.setHint(hintName, entityGraph);
  458.             }
  459.  
  460.             println("final TestEntity test = tq.getSingleResult();");
  461.             final TestEntity test = tq.getSingleResult();
  462.  
  463.             //System.out.print(id + '\t' + graph + '\t' + fetch);
  464.             println("test.reportInitializationState();");
  465.  
  466.             test.reportInitializationState();
  467.  
  468.             if (printAll) {
  469.                 println("test.printAll();");
  470.                 test.printAll();
  471.             }
  472.  
  473.             println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  474.         }
  475.     }
  476.  
  477.     private static Session getNewHibernateSession() {
  478.         return null; // Implement your own
  479.     }
  480.  
  481.     public static vid testLoading() {
  482.         for (String h: new String[]{"javax.persistence.fetchgraph", "javax.persistence.loadgraph"}) {
  483.             for (int graph = 0; graph < 4; graph++) {
  484.                 testLoading(100, graph, 0, true, h);
  485.             }
  486.         }
  487.  
  488.         for (int fetch = 0; fetch < 4; fetch++) {
  489.             testLoading(100, 0, fetch, true, null);
  490.         }
  491.     }    
  492. }
  493.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement