1.1.24. fejezet, ORM

Hibernate trükkök

SQLite adatforrás

    <datasource jndi-name="java:jboss/datasources/Homework4SQLiteDS"
        pool-name="Homework4SQLite" enabled="true"
        use-java-context="true">
        <connection-url>jdbc:sqlite:${jboss.home.dir}/welcome-content/config.db</connection-url>
        <driver>sqlite</driver>
    </datasource>

Proxy mezők lecserélése objektum példányra

@ViewScoped
class ViewScopedBean {
...
    @Inject
    private StatelessBean service;
---
    @Resource
    private UserTransacrion ut;
...
    public void fetch() {
      try{
         ut.begin;
...
         entity = service.find(1L);
...
         deproxyEntity(entity);
...
      } finally {
         ut.rollback;
      }
...
    }
...
    public static void deproxyhEntity(Object object) {
        Field[] fields = object.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                Object fieldValue = field.get(object);
                if (fieldValue instanceof HibernateProxy) {
                    Object detachedFieldValue = ((HibernateProxy) fieldValue).getHibernateLazyInitializer().getImplementation();
                    field.set(object, detachedFieldValue);
                }
            } catch (IllegalArgumentException | IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
...
}
 
@Stateless
class StatelessBean {
...
  @Inject
  private EntityManager em;
...
  public A find(Long id) {
    A entity = em.find(A.class, id);
  }
....
}

(@Stateless objektum metódushívása legyen a felhasználói tranzakción belül!)

Egyéb tranzakciókkal kapcsolatos feladat a
batch insert/update