• Is there a simpler way to map Hibernate?

    From e.d.programmer@gmail.com@21:1/5 to All on Thu Aug 31 04:49:34 2023
    I'm reading tables by putting @Entity and @Table on classes. For each of those I have to add a mapping tag in the session-factory tag in a hibernate.cfg.xml file in the src/main/resources folder, then copy this tag into the same file in src/test/
    resources? Is there a way to share regular mappings with unit testing? Is there a way to automatically include all entities, or a reason I shouldn't want to include all?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?Q?Arne_Vajh=c3=b8j?=@21:1/5 to e.d.pro...@gmail.com on Thu Aug 31 18:46:23 2023
    On 8/31/2023 7:49 AM, e.d.pro...@gmail.com wrote:
    I'm reading tables by putting @Entity and @Table on classes. For each
    of those I have to add a mapping tag in the session-factory tag in a hibernate.cfg.xml file in the src/main/resources folder, then copy
    this tag into the same file in src/test/resources? Is there a way to
    share regular mappings with unit testing? Is there a way to
    automatically include all entities, or a reason I shouldn't want to
    include all?

    If you use JPA annotations why do you need a Hibernate mapping
    file at all?

    Just use annotations @Id, @Column, @OneToMany on properties
    to specify all the details on how to access database.

    Arne

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From e.d.programmer@gmail.com@21:1/5 to All on Fri Sep 1 04:08:13 2023
    If you use JPA annotations why do you need a Hibernate mapping
    file at all?

    Just use annotations @Id, @Column, @OneToMany on properties
    to specify all the details on how to access database.

    Arne
    I am using those annotations, and if I don't have the mapping tags in the cfg file, as soon as I call createQuery I get "... is not mapped"

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?Q?Arne_Vajh=c3=b8j?=@21:1/5 to e.d.pro...@gmail.com on Fri Sep 1 08:27:15 2023
    On 9/1/2023 7:08 AM, e.d.pro...@gmail.com wrote:
    If you use JPA annotations why do you need a Hibernate mapping
    file at all?

    Just use annotations @Id, @Column, @OneToMany on properties
    to specify all the details on how to access database.

    I am using those annotations, and if I don't have the mapping tags in
    the cfg file, as soon as I call createQuery I get "... is not
    mapped"

    What do you have in persistence.xml?

    Arne

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From e.d.programmer@gmail.com@21:1/5 to All on Fri Sep 1 05:49:08 2023
    What do you have in persistence.xml?

    Arne
    I don't have a persistence.xml in this project, just the hibernatecfg.xml.

    I have another project using Hibernate through the Flowable framework which has a persistence.xml with persistence-unit tags containing class tags for every entity.
    This one is just directly referencing Hibernate with session-factory.
    new MetadataSources(new StandardServiceRegistryBuilder().configure().build()).getMetadataBuilder().build().getSessionFactoryBuilder().build()
    try (Session session = getMyFactory().openSession();){

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?Q?Arne_Vajh=c3=b8j?=@21:1/5 to e.d.pro...@gmail.com on Fri Sep 1 09:06:35 2023
    On 9/1/2023 8:49 AM, e.d.pro...@gmail.com wrote:
    What do you have in persistence.xml?

    I don't have a persistence.xml in this project, just the hibernatecfg.xml.

    I have another project using Hibernate through the Flowable framework which has a persistence.xml with persistence-unit tags containing class tags for every entity.
    This one is just directly referencing Hibernate with session-factory.
    new MetadataSources(new StandardServiceRegistryBuilder().configure().build()).getMetadataBuilder().build().getSessionFactoryBuilder().build()
    try (Session session = getMyFactory().openSession();){

    So you are not using JPA with Hibernate as implementation - you
    are using Hibernate with JPA annotations.

    I believe that is supposed to work, but most people changed
    from Hibernate with XML to JPA with annotations like 15 years
    ago.

    I have no idea about how Hibernate with JPA annotations.
    work.

    Any chance that you could change to JPA?

    Arne

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From e.d.programmer@gmail.com@21:1/5 to All on Fri Sep 1 06:25:36 2023
    So you are not using JPA with Hibernate as implementation - you
    are using Hibernate with JPA annotations.

    I believe that is supposed to work, but most people changed
    from Hibernate with XML to JPA with annotations like 15 years
    ago.

    I have no idea about how Hibernate with JPA annotations.
    work.

    Any chance that you could change to JPA?

    Arne
    What is "JPA with Hibernate" versus "Hibernate with JPA annotations"? Is this just about using persistence.xml files with EntityManager? That still requires declaring every entity class in the xml file and duplicating that for test? What is the advantage?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?Q?Arne_Vajh=c3=b8j?=@21:1/5 to e.d.pro...@gmail.com on Fri Sep 1 09:37:42 2023
    On 9/1/2023 9:25 AM, e.d.pro...@gmail.com wrote:
    So you are not using JPA with Hibernate as implementation - you are
    using Hibernate with JPA annotations.

    I believe that is supposed to work, but most people changed from
    Hibernate with XML to JPA with annotations like 15 years ago.

    I have no idea about how Hibernate with JPA annotations. work.

    Any chance that you could change to JPA?

    What is "JPA with Hibernate" versus "Hibernate with JPA annotations"?
    Is this just about using persistence.xml files with EntityManager?
    That still requires declaring every entity class in the xml file and duplicating that for test? What is the advantage?

    Yes. Basically:

    EntityManagerFactory emf = Persistence.createEntityManagerFactory(confnam); EntityManager em = emf.createEntityManager();

    instead of:

    SessionFactory sf = new Configuration().configure(new File(conffile)).buildSessionFactory();
    Session s = sf.openSession();

    (your setup code is a little bit more advanced but above
    is the basic version)

    I see two advantages.

    1) You switch to a mode that almost everyone else is using
    so a lot better support and a lot easier to get help.

    2) It is correct that the most common approach is to
    have a persistence.xml with:

    <class>p.C1</class>
    <class>p.C2</class>
    <class>p.C3</class>
    <exclude-unlisted-classes/>

    but you could try with:

    <exclude-unlisted-classes>false</exclude-unlisted-classes>

    I am not an expert in the finer points of JPA, but I remember
    it as that false should work in Java EE context and that it
    is not guaranteed to work in SE context but usually does.

    Arne

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From e.d.programmer@gmail.com@21:1/5 to All on Fri Sep 1 12:48:21 2023
    but you could try with:

    <exclude-unlisted-classes>false</exclude-unlisted-classes>

    I am not an expert in the finer points of JPA, but I remember
    it as that false should work in Java EE context and that it
    is not guaranteed to work in SE context but usually does.

    Arne
    I started trying to implement this, didn't get time to test today, I'll let you know.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From e.d.programmer@gmail.com@21:1/5 to All on Tue Sep 5 12:34:45 2023
    Yes. Basically:

    EntityManagerFactory emf = Persistence.createEntityManagerFactory(confnam); EntityManager em = emf.createEntityManager();

    instead of:

    SessionFactory sf = new Configuration().configure(new File(conffile)).buildSessionFactory();
    Session s = sf.openSession();
    ...

    Arne

    This is confusing. I've been implementing the openSession using try-with-resources.
    org.hibernate.Session extends javax.persistence.EntityManager (I'm using Hibernate 5x since 6x requires newer Java, stuck using Java 8 for now) as well as Autocloseable.
    EntityManager is not autocloseable, though if I'm reading this right, the first thread I found on stackoverflow suggests closing it after every use unless you're working with very small data.
    If I'm creating the EntityManager directly instead of the Session, should I create my own wrapper to make it auto-closeable? Is there a good use case for not closing it? It causes memory leaks if you don't close it when you're done with it unless you're
    re-using the same one instead of creating a new one each time? I assume they can still be chained, like I create one in a DAO to access one DTO and while it's open I call a method of a different DAO to access a different DTO and it creates it's own
    EntityManager?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?Q?Arne_Vajh=c3=b8j?=@21:1/5 to e.d.pro...@gmail.com on Sun Sep 10 20:48:21 2023
    On 9/5/2023 3:34 PM, e.d.pro...@gmail.com wrote:
    Yes. Basically:

    EntityManagerFactory emf =
    Persistence.createEntityManagerFactory(confnam); EntityManager em =
    emf.createEntityManager();

    instead of:

    SessionFactory sf = new Configuration().configure(new
    File(conffile)).buildSessionFactory(); Session s =
    sf.openSession();
    ...

    This is confusing. I've been implementing the openSession using try-with-resources. org.hibernate.Session extends javax.persistence.EntityManager (I'm using Hibernate 5x since 6x
    requires newer Java, stuck using Java 8 for now) as well as
    Autocloseable. EntityManager is not autocloseable,

    If Session extends EntityManager you should be able to
    use the JPA methods on Session and just use Session auto closeable
    feature.

    Arne

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From e.d.programmer@gmail.com@21:1/5 to All on Mon Sep 11 04:19:26 2023
    This is confusing. I've been implementing the openSession using try-with-resources. org.hibernate.Session extends javax.persistence.EntityManager (I'm using Hibernate 5x since 6x
    requires newer Java, stuck using Java 8 for now) as well as
    Autocloseable. EntityManager is not autocloseable,
    If Session extends EntityManager you should be able to
    use the JPA methods on Session and just use Session auto closeable
    feature.

    Arne

    So just use EntityManagerFactory instead of SessionFactory, then just call .unwrap(Session.class) and nothing needs to change but the factory?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From e.d.programmer@gmail.com@21:1/5 to All on Mon Sep 11 05:23:25 2023
    If Session extends EntityManager you should be able to
    use the JPA methods on Session and just use Session auto closeable
    feature.

    Arne

    So I'm trying this.
    I deleted hibernate.cfg.xml. I created src/main/resources/META-INF/hibernate.xml.
    My hibernate.xml has a persistence-unit with the <exclude-unlisted-classes>false and 5 properties defining the JDBC connection, no class specific mappings.
    I read on one S/O that the unwrap(Session.class) should return a Session, it didn't.
    We have a final class with a private static factory. I changed that factory from the hibernate factory to EntityManagerFactory.
    Now it has a static method I changed to:
    public static Session getHibernateSession() {
    if (factory == null) {
    factory = Persistence.createEntityManagerFactory("mypersistenceunit.jpa");
    }
    Object delegate = factory.createEntityManager().getDelegate();
    if (delegate instanceof Session) {
    return (Session) delegate;
    }
    return factory.unwrap(Session.class);
    }
    We call this method from try-with-resources. This is working. The next apparent question is do we want to be referencing org.hibernate.Session? One site says use EntityManager to keep it generic because there are framework alternatives to Hibernate ("
    like EclipseLink or TopLink"?). It seems Hibernate is supposed to be "under the hood" so we have few if any references to it outside of the pom and persistence.xml.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)