Hibernate Tutorial Net Beans7.1
This simple tutorials will illustrate how we can use Hibernate frame work to access a MySQL database in net beans 7.1. Here I'm not going to explain this with more details, because It is very difficult to add all the details. I assume that you have enough knowledge in relevant domains.For basics click here. Here you can find important pieces of codes.
Details about target MySQL database which I'm going to access
Database name : combo
Table name : items
1. Configuring hibernate
Here is the code
Highlighted lines(5&6) configure username property & password property. Replace them with yours.
2. POJO class.
3. Hibernate mapping file
If you are using auto generate primary keys replace "assigned" in line 4 with "native".
4. Setting values
If your database & everything fine you should be able to see a result like this
. . .
Run SELECT * FROM items; on items table to see updated table:
If you find this post helpful don't forget to leave a comment.
This simple tutorials will illustrate how we can use Hibernate frame work to access a MySQL database in net beans 7.1. Here I'm not going to explain this with more details, because It is very difficult to add all the details. I assume that you have enough knowledge in relevant domains.For basics click here. Here you can find important pieces of codes.
Details about target MySQL database which I'm going to access
Database name : combo
Table name : items
1. Configuring hibernate
Here is the code
com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/combo root root123 10 org.hibernate.dialect.MySQLDialect thread org.hibernate.cache.NoCacheProvider true update
Highlighted lines(5&6) configure username property & password property. Replace them with yours.
2. POJO class.
package firsthibernetapp; public class Item { private String icode; private String idiscription; private int iprice; private int iid; public void setIcode(String icode) { this.icode = icode; } public void setIdiscription(String idiscription) { this.idiscription = idiscription; } public void setIid(int iid) { this.iid = iid; } public void setIprice(int iprice) { this.iprice = iprice; } public String getIcode() { return icode; } public String getIdiscription() { return idiscription; } public int getIid() { return iid; } public int getIprice() { return iprice; } }
3. Hibernate mapping file
<id
column
=
"itemId"
name
=
"iid"
type
=
"int"
>
This line maps to the primary key of the database table.If you are using auto generate primary keys replace "assigned" in line 4 with "native".
4. Setting values
package firsthibernetapp; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class FirstHibernetApp { public static void main(String[] args) { Session session = null; try{ SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); session =sessionFactory.openSession(); session.beginTransaction(); Item item = new Item(); System.out.println("Inserting Record"); item.setIid(23); item.setIcode("FX00011D"); item.setIdiscription("Combo"); item.setIprice(1500); session.save(item); System.out.println("Done"); session.getTransaction().commit(); }catch(Exception e){ System.out.println(e.getMessage()); } finally{ session.flush(); session.close(); } } }
If your database & everything fine you should be able to see a result like this
. . .
Run SELECT * FROM items; on items table to see updated table:
If you find this post helpful don't forget to leave a comment.
Nice Article :D
ReplyDelete