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 mappingI 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"
file at all?
Just use annotations @Id, @Column, @OneToMany on properties
to specify all the details on how to access database.
Arne
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?I don't have a persistence.xml in this project, just the hibernatecfg.xml.
Arne
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 - youWhat 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?
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
So you are not using JPA with Hibernate as implementation - you areWhat is "JPA with Hibernate" versus "Hibernate with JPA annotations"?
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?
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?
but you could try with:I started trying to implement this, didn't get time to test today, I'll let you know.
<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
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
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,
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 6xIf Session extends EntityManager you should be able to
requires newer Java, stuck using Java 8 for now) as well as
Autocloseable. EntityManager is not autocloseable,
use the JPA methods on Session and just use Session auto closeable
feature.
Arne
If Session extends EntityManager you should be able to
use the JPA methods on Session and just use Session auto closeable
feature.
Arne
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 365 |
Nodes: | 16 (2 / 14) |
Uptime: | 06:46:58 |
Calls: | 7,785 |
Files: | 12,914 |
Messages: | 5,750,442 |