Newsletter |
Hibernate Hello World Program With Annotations
Hibernate » on Aug 30, 2011 { 13 Comments } By Sivateja
Folks we will see one simple program with hibernate annotations, let us take inserting a record [ saving one object ] into the database application. And remember in the annotations no need to write mapping xml, hope you remember the previous sessions 🙂
Files required..
- Product.java [ our pojo class ]
- ClientForSave.java
- hibernate.cfg.xml
Product.java
package str; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "student_talbe") public class Product{ @Id @Column(name="proid") private int productId; @Column(name="proName", length=10) private String proName; @Column(name="price") private double price; public void setProductId(int productId) { this.productId = productId; } public int getProductId() { return productId; } public void setProName(String proName) { this.proName = proName; } public String getProName() { return proName; } public void setPrice(double price) { this.price = price; } public double getPrice() { return price; } }
ClientForSave.java
package str; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; public class ClientForSave { public static void main(String[] args) { AnnotationConfiguration cfg=new AnnotationConfiguration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); Product p=new Product(); p.setProductId(105); p.setProName("java4s"); p.setPrice(15000); Transaction tx = session.beginTransaction(); session.save(p); System.out.println("Object saved successfully using annotations.....!!"); tx.commit(); session.close(); factory.close(); } }
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver </property> <property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property> <property name="connection.username">system</property> <property name="connection.password">admin</property> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping class="str.Product" /> </session-factory> </hibernate-configuration>
And friends, no need to explain there i think. Please refer previous introduction sessions on annotations in case you have any doubt.
Output
You Might Also Like
::. About the Author .:: | ||
Comments
13 Responses to “Hibernate Hello World Program With Annotations”
Hello Java4s,
Am getting exception in this program.
Showing exception as
Exception in thread “main” org.hibernate.MappingException: Unknown entity: str.Product
Please help!
I got solution for the exception which i was facing.
Exception in thread “main” org.hibernate.MappingException: Unknown entity: str.Product
If we add the two lines in the hibernate.cfg file then i did not got that exception again and the program got executed successfully..
@Vaseem
Hmm… str.Product !
Vaseem what are those 2 lines you added into hibernate.cfg ?
The program can get executed even with one line..
that line is..
When we are not using hbm.xml file(mapping file), then we have to give fully qualified name of the annotated class to the mapping element.
i need some more emphasis on NamedQuery theory and Example like as development mode i can use that..if you can do… would b better for me…Thanks Java4s
In the hibernate.cfg.xml file there is no mapping for pojo class…we need to map the pojo class as we are not using hbm.xml
@Aalam
Yeah in the 18th line, we must specify the mapping class name you are correct, that should be class=”str.Product”. Corrected..!!
Thanks Aalam 🙂
Db table name : student_table
Table name mentioned as @Table(name = “student_talbe”)
table name mentioned in are differnt. How will this program work ? 🙁
To stephen: Hi Stephen,Hibernate will create table automatically as per given query in “update” in hibernate.cfg.xml file.Here you can also use “create” instead of update.
Sir as you said we use annaotations to avoid using mapping xml i.e instead of using mapping xml we r using annotations.
My question is in your herbernate.cfg.xml file Why you are again using
?????
please answer me!!!!!!
Hi,Java4s i have a solution for Hibernate Mapping Exception
Solution is:
Just set in your client class 15th line
cfg.addAnnotatedClass(Product.class);
No need to set any
<mapping class="str.Product"/> in your configrtation FIle
To Execute above program.
We need to create table in data base or not ?
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/LogManager
i use hibernet 5 and jdk 8