Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #40000 > unrolled thread
| Started by | "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> |
|---|---|
| First post | 2023-08-31 04:49 -0700 |
| Last post | 2023-09-11 05:23 -0700 |
| Articles | 13 — 2 participants |
Back to article view | Back to comp.lang.java.programmer
Is there a simpler way to map Hibernate? "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-08-31 04:49 -0700
Re: Is there a simpler way to map Hibernate? Arne Vajhøj <arne@vajhoej.dk> - 2023-08-31 18:46 -0400
Re: Is there a simpler way to map Hibernate? "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-09-01 04:08 -0700
Re: Is there a simpler way to map Hibernate? Arne Vajhøj <arne@vajhoej.dk> - 2023-09-01 08:27 -0400
Re: Is there a simpler way to map Hibernate? "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-09-01 05:49 -0700
Re: Is there a simpler way to map Hibernate? Arne Vajhøj <arne@vajhoej.dk> - 2023-09-01 09:06 -0400
Re: Is there a simpler way to map Hibernate? "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-09-01 06:25 -0700
Re: Is there a simpler way to map Hibernate? Arne Vajhøj <arne@vajhoej.dk> - 2023-09-01 09:37 -0400
Re: Is there a simpler way to map Hibernate? "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-09-01 12:48 -0700
Re: Is there a simpler way to map Hibernate? "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-09-05 12:34 -0700
Re: Is there a simpler way to map Hibernate? Arne Vajhøj <arne@vajhoej.dk> - 2023-09-10 20:48 -0400
Re: Is there a simpler way to map Hibernate? "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-09-11 04:19 -0700
Re: Is there a simpler way to map Hibernate? "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> - 2023-09-11 05:23 -0700
| From | "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> |
|---|---|
| Date | 2023-08-31 04:49 -0700 |
| Subject | Is there a simpler way to map Hibernate? |
| Message-ID | <015e1e01-2e15-49bf-ae5e-07470e4e3d2bn@googlegroups.com> |
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?
[toc] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2023-08-31 18:46 -0400 |
| Message-ID | <ucr57u$3fa1g$1@dont-email.me> |
| In reply to | #40000 |
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
[toc] | [prev] | [next] | [standalone]
| From | "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> |
|---|---|
| Date | 2023-09-01 04:08 -0700 |
| Message-ID | <c7dbfc66-c143-45f0-8501-50d9aa90e3b2n@googlegroups.com> |
| In reply to | #40001 |
> 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"
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2023-09-01 08:27 -0400 |
| Message-ID | <ucslb3$3p6nj$1@dont-email.me> |
| In reply to | #40002 |
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
[toc] | [prev] | [next] | [standalone]
| From | "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> |
|---|---|
| Date | 2023-09-01 05:49 -0700 |
| Message-ID | <f0746a5b-9125-4055-b2a4-de230c621f6cn@googlegroups.com> |
| In reply to | #40003 |
> 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();){
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2023-09-01 09:06 -0400 |
| Message-ID | <ucsnkr$3pftv$1@dont-email.me> |
| In reply to | #40004 |
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
[toc] | [prev] | [next] | [standalone]
| From | "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> |
|---|---|
| Date | 2023-09-01 06:25 -0700 |
| Message-ID | <69105518-21d7-468c-9774-e9ffb497677fn@googlegroups.com> |
| In reply to | #40005 |
> 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?
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2023-09-01 09:37 -0400 |
| Message-ID | <ucspf7$3ppii$1@dont-email.me> |
| In reply to | #40006 |
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
[toc] | [prev] | [next] | [standalone]
| From | "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> |
|---|---|
| Date | 2023-09-01 12:48 -0700 |
| Message-ID | <0626d57c-7e93-42dc-a807-8310b7badc7cn@googlegroups.com> |
| In reply to | #40007 |
> 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.
[toc] | [prev] | [next] | [standalone]
| From | "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> |
|---|---|
| Date | 2023-09-05 12:34 -0700 |
| Message-ID | <74cf0893-49c0-46dc-8381-92244f60bde1n@googlegroups.com> |
| In reply to | #40007 |
> 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?
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2023-09-10 20:48 -0400 |
| Message-ID | <udlo4l$otip$1@dont-email.me> |
| In reply to | #40010 |
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
[toc] | [prev] | [next] | [standalone]
| From | "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> |
|---|---|
| Date | 2023-09-11 04:19 -0700 |
| Message-ID | <4d753bdf-e14f-416b-97c2-254de83c55b7n@googlegroups.com> |
| In reply to | #40011 |
> > 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?
[toc] | [prev] | [next] | [standalone]
| From | "e.d.pro...@gmail.com" <e.d.programmer@gmail.com> |
|---|---|
| Date | 2023-09-11 05:23 -0700 |
| Message-ID | <467de546-1da2-4943-83f8-4dd760f7c136n@googlegroups.com> |
| In reply to | #40011 |
> 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web