Hello,
I am working with Hibernate 6.5 with Maven, Oracle 19c, Java 11. My work is based on a hibernate application with hibernate 3.
I have good java application experience, but almost 0 hibernate 2 months ago. My headache is that in many video or document, people talk about with different versions, some functions are deprecated, or not available.
Suppose I set the pom.xml, hibernate.cfg.xml, the entity with annotation, I have a utility file create sessionFactory as following:
standardServiceRegistry = new StandardServiceRegistryBuilder().configure().build();
MetadataSources metadataSources = new MetadataSources( standardServiceRegistry );
Metadata metadata = metadataSources.getMetadataBuilder().build();
sessionFactory = metadata.getSessionFactoryBuilder().build();
Start at mySession, I am looking for samples about:
use mySession to create all different query to insert, update, delete, fetch list of results of table1 join table2 (join may not by primary key), createNativeQuery, criteria api.
Could you please forward good examples or good books?
Thank you in advance,
Eric
We have recently published an introductory guide targeted at new users of Hibernate ORM: An Introduction to Hibernate 6. Going through that should give you a pretty good idea of how to get started and illustrate how most of the basic functionalities work.
To get more in-depth details about specific features and intricacies of the framework you can use our Hibernate ORM User Guide, which is structured as more of a reference documentation.
Hello mbladel,
Sorry for bothering you.
My Hibernate application has a few queries worked with hibernate 6.5. When I try Composite Primary Keys, I followed the example at Hibernate ORM User Guide
section 3.7.2 and 3.7.3, I got error when the first getSessionFactory() is called:.
in hibernate.cfg.xm (no hbm.xml file):
...... mapping class="com.gov.bart.maris.entity.MrsVehStatHist"/> ......
in Entity:
@Entity(name = "MrsVehStatHist")
@Table(name = "MRS_VEH_STAT_HIST")
@IdClass(MrsVehStatHistPK.class)
public class MrsVehStatHist { // implements java.io.Serializable {
//private static final long serialVersionUID = 1L;
//@EmbeddedId
//private MrsVehStatHistPK comp_id;
@Column(name = "CAR_NO")
private String carNo;
@Column(name = "TS_STAT_CHG")
private Timestamp tsStatChg;
// skipped many other fields, and the set and get functions on all
//the fields include the primary key members carNo, tsStatChg
in the Key class MrsVehStatHistPK:
public class MrsVehStatHistPK implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private String carNo;
private Timestamp tsStatChg;
public MrsVehStatHistPK(String carNo, Timestamp tsStatChg) {
this.carNo = carNo;
this.tsStatChg = tsStatChg;
}
@Column(name = "CAR_NO")
public String getCarNo() {
return this.carNo;
}
public void setCarNo(String carNo) {
this.carNo = carNo;
}
@Column(name = "TS_STAT_CHG")
public Timestamp getTsStatChg() {
return this.tsStatChg;
}
public void setTsStatChg(Timestamp tsStatChg) {
this.tsStatChg = tsStatChg;
}
Error:
Initial SessionFactory creation failed.org.hibernate.AnnotationException: Entity ‘com.gov.bart.maris.entity.MrsVehIncdStat’ has no identifier (every ‘@Entity’ class must declare or inherit at least one ‘@Id’ or ‘@EmbeddedId’ property)
org.hibernate.AnnotationException: Entity ‘com.gov.bart.maris.entity.MrsVehIncdStat’ has no identifier (every ‘@Entity’ class must declare or inherit at least one ‘@Id’ or ‘@EmbeddedId’ property)
at org.hibernate.boot.model.internal.InheritanceState.determineDefaultAccessType(InheritanceState.java:279)
at org.hibernate.boot.model.internal.InheritanceState.getElementsToProcess(InheritanceState.java:215)
I tried different ways, and tried @EmbeddedId.
Please help. Thanks,
Eric
The error is pretty self-explanatory: all entity classes must have a field annotated with either @Id, for basic primary keys, or @EmbeddedId, for composite identifiers (there’s also @IdClasses but let’s ignore them for now). In you code I see the class MrsVehStatHist has an @EmbeddedId field but that’s commented out, uncommenting that should do the trick for you.
Also: you should annotate the MrsVehStatHistPK class as @Embeddable, to notify Hibernate that class will be used as a component in your applicaiton. Finally, you’re using AccessType.FIELD in your entity class, by placing annotation on fields, while in the embeddable class you’re implying AccessType.PROPERTY by placing @Column annotations on the getters: this won’t work as embeddables inherit the access strategy from the entities they’re contained in, so you should also annotate fields there. Find out more about access strategies here.