හා හා! දැං තියෙන වැඩ පැත්තකින් දාලා වරෙව්!
STS open කරගෙන, MySQL open කරගෙන, database connection හදාගෙන ලෑස්ති වෙයව්!


හොඳායි....
අද අපිට කරන්න තියෙන්නේ, Annotations use කරලා Hibernate වලින් කොහොමද Database එකට කොක්කක් ගහගන්නේ කියලා හොයලා බලන්න.
Annotation එකක් කියන්නේ මොකද්ද කියලා කට්ටිය දන්නවා ඇති කියලා හිතනවා.
ඔය නිල් පාටින් කොටු කරලා තියෙන්නේ Annotations . පොඩි හරි idea එකක් ගන්න පුළුවන්නේ? කොහොමද මේක use වෙන්නේ කියලා?
මේකට අපි කලින් හදපු project එකම use කරනවා මම. බොලාලට ඉතිං කැමැත්තක් කොරන්ඩ පුලුවනි! අළුතෙන්ම එකක් හදනවානම් තමයි ඉතිං හොඳ.
ඔන්න වැඩේට බැස්සා!
චූටි චූටි වෙනස්කම් කීපයයි කරන්න තියෙන්නේ.
1 - Add Hibernate annotation dependency.
කලින් පාවිච්චි කරපු dependencies වලට අමතරව අපිට තව මේ dependency දෙකත් add කරගන්න වෙනවා!
i - hibernate-annotations --> 3.3.0.GA
ii - hibernate-commons-annotations --> 3.3.0.GA
ඔය? පේනවනේ?
ඔන්න මම add කරගත්තා.
2 - Update HibernateUtil.java
මතකයිනේ? එදා අපි හදා ගත්තා HibernateUtil කියලා class එකක්?
ඔව් ඔව්! ඔය තියෙන්නේ...
ඒක open කරලා line number වලට 13 ට ගිහිල්ලා බැලුවාම උඹලට මෙන්න මෙහෙම code කෑල්ලක් පේන්න ඕනේ.
return new Configuration().configure().buildSessionFactory();
තියෙනවා?
then good!
Folks, අපි දැන් ඒක replace කරගන්න යන්නේ!
මෙන්න මේ code කෑල්ලෙන්!
return new AnnotationConfiguration().configure().buildSessionFactory();
වෙනස බලාපල්ලා, කලින් code එක out of fashion . ඒක තිබ්බේ xml use කරලා configure කරන්න!
දැං code එක තියෙන්නේ, Annotation use කරලා configure කරන්න!
3 - Update Model class
මතකයිනේ? අපි එදා අපේ modal class එක හැදුවා Stock කියලා? ආන්න ඒකත් edit කරගන්න ඕනේ.
මෙන්න මේ Stock class එක (modal class එක) ට තමයි අපි annotations දාගන්නේ!
හරි! මෙන්න මෙහෙමයි edit වෙන්නේ.
package com.inventory.common;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table(name = "stock", catalog = "inventory", uniqueConstraints = {
@UniqueConstraint(columnNames = "STOCK_NAME"),
@UniqueConstraint(columnNames = "STOCK_CODE") })
public class Stock implements java.io.Serializable {
private Integer stockId;
private String stockCode;
private String stockName;
public Stock() {
}
public Stock(String stockCode, String stockName) {
this.stockCode = stockCode;
this.stockName = stockName;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "STOCK_ID", unique = true, nullable = false)
public Integer getStockId() {
return this.stockId;
}
public void setStockId(Integer stockId) {
this.stockId = stockId;
}
@Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10)
public String getStockCode() {
return this.stockCode;
}
public void setStockCode(String stockCode) {
this.stockCode = stockCode;
}
@Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20)
public String getStockName() {
return this.stockName;
}
public void setStockName(String stockName) {
this.stockName = stockName;
}
}
** The import javax.persistence cannot be resolved කියලා errors පෙන්නන්න ගත්තොතින් ඔය පහල තියෙන jar එකත් add කරගනිල්ලා.
- බොහෝ දුරට add කරගන්න වෙයි උඹලට.
- ejb3-persistence --> 1.0.2.GA
4 - Delete existing Hibernate XML mapping file
Stock.hbm.xml file එක මතකයිනේ? ඔව් ඔව්! mapping file එක තමයි බොලව්. ඔන්න ඔය "හු" යන්න delete කරපියව්! තව දුරටත් ඕකෙන් වැඩක් වෙන්නේ නෑ
5 - Update Hibernate configuration file.
දැං Update Hibernate configuration file එක ඔය පහල තියෙන විදියට edit කරගන්න ඕනේ.
මෙන්න මේ විදියට තමයි දැං Update Hibernate configuration file එක පේන්න තියෙන්නේ.
මේ? දැක්කද full project structure එක?
ඔන්න ඒකත් හරි!
Run කරලා බලන එකයි තියෙන්නේ.
App class උඩ right click කලා --> Run as --> Java application
පේනවනේ? මම අළුත් data ටිකක් දාගෙන තියෙන්නේ save කරගන්න.
Here is the result..
ඔය රතු පාටින් තියෙන්නේ මම අළුතෙන් දාපු data ටික
මේ වෙනස සැපයි කියලා හිතනවා උඹලට.
** එහෙම නැත්තන් උඹලට hbm.xml ලිය ලිය බලු කට්ටක් කන්න වෙන්නේ. දැං වැඩේ ලේසිනේ? Model class එකෙන්ම වැඩේ ගොඩ.
හරි! ඊළඟට තියෙන්නේ ටිකක් ලොකු පිම්මක් පනින්න. අපි Maven ගැන ඉගෙන ගන්න යන්නේ.
Maven Athapaththu??


නෑ නෑ!

මේක dependency managment and build automation tool එකක්.
ඒ උනාට Maven Athapaththu වගේම තාක්ෂණයෙන් බොහෝම අනූනයි.
ඔන්න දැන් අපි ටිකෙන් ටික more advanced technologies use කරන්න පටන් ගන්නවා.
ඉතිං basic හරියට නැති කට්ටිය basic brush-up කරගෙනම ඊළඟ lessons වලට join උනොත් තමයි හොඳ. 
මේකට උඹලගේ කැපවීම අනිවාර්යයි! මට පුලුවන් බොලාට මඟ පෙන්නන්න විතරයි!
එහෙනම් හැමෝටම ජය!
කලින් දාපු lessons ටික මෙතනින් බලල go through කරපල්ලා
STS open කරගෙන, MySQL open කරගෙන, database connection හදාගෙන ලෑස්ති වෙයව්!



හොඳායි....
අද අපිට කරන්න තියෙන්නේ, Annotations use කරලා Hibernate වලින් කොහොමද Database එකට කොක්කක් ගහගන්නේ කියලා හොයලා බලන්න.

Annotation එකක් කියන්නේ මොකද්ද කියලා කට්ටිය දන්නවා ඇති කියලා හිතනවා.
ඔය නිල් පාටින් කොටු කරලා තියෙන්නේ Annotations . පොඩි හරි idea එකක් ගන්න පුළුවන්නේ? කොහොමද මේක use වෙන්නේ කියලා?
මේකට අපි කලින් හදපු project එකම use කරනවා මම. බොලාලට ඉතිං කැමැත්තක් කොරන්ඩ පුලුවනි! අළුතෙන්ම එකක් හදනවානම් තමයි ඉතිං හොඳ.

ඔන්න වැඩේට බැස්සා!

චූටි චූටි වෙනස්කම් කීපයයි කරන්න තියෙන්නේ.

1 - Add Hibernate annotation dependency.
කලින් පාවිච්චි කරපු dependencies වලට අමතරව අපිට තව මේ dependency දෙකත් add කරගන්න වෙනවා!
i - hibernate-annotations --> 3.3.0.GA
ii - hibernate-commons-annotations --> 3.3.0.GA
ඔය? පේනවනේ?
ඔන්න මම add කරගත්තා.
2 - Update HibernateUtil.java
මතකයිනේ? එදා අපි හදා ගත්තා HibernateUtil කියලා class එකක්?
ඔව් ඔව්! ඔය තියෙන්නේ...

ඒක open කරලා line number වලට 13 ට ගිහිල්ලා බැලුවාම උඹලට මෙන්න මෙහෙම code කෑල්ලක් පේන්න ඕනේ.
return new Configuration().configure().buildSessionFactory();
තියෙනවා?
then good!
Folks, අපි දැන් ඒක replace කරගන්න යන්නේ!
මෙන්න මේ code කෑල්ලෙන්!
return new AnnotationConfiguration().configure().buildSessionFactory();
වෙනස බලාපල්ලා, කලින් code එක out of fashion . ඒක තිබ්බේ xml use කරලා configure කරන්න!
දැං code එක තියෙන්නේ, Annotation use කරලා configure කරන්න!
3 - Update Model class
මතකයිනේ? අපි එදා අපේ modal class එක හැදුවා Stock කියලා? ආන්න ඒකත් edit කරගන්න ඕනේ.
මෙන්න මේ Stock class එක (modal class එක) ට තමයි අපි annotations දාගන්නේ!
හරි! මෙන්න මෙහෙමයි edit වෙන්නේ.
package com.inventory.common;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
@Entity
@Table(name = "stock", catalog = "inventory", uniqueConstraints = {
@UniqueConstraint(columnNames = "STOCK_NAME"),
@UniqueConstraint(columnNames = "STOCK_CODE") })
public class Stock implements java.io.Serializable {
private Integer stockId;
private String stockCode;
private String stockName;
public Stock() {
}
public Stock(String stockCode, String stockName) {
this.stockCode = stockCode;
this.stockName = stockName;
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "STOCK_ID", unique = true, nullable = false)
public Integer getStockId() {
return this.stockId;
}
public void setStockId(Integer stockId) {
this.stockId = stockId;
}
@Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10)
public String getStockCode() {
return this.stockCode;
}
public void setStockCode(String stockCode) {
this.stockCode = stockCode;
}
@Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20)
public String getStockName() {
return this.stockName;
}
public void setStockName(String stockName) {
this.stockName = stockName;
}
}
** The import javax.persistence cannot be resolved කියලා errors පෙන්නන්න ගත්තොතින් ඔය පහල තියෙන jar එකත් add කරගනිල්ලා.
- බොහෝ දුරට add කරගන්න වෙයි උඹලට.
- ejb3-persistence --> 1.0.2.GA
4 - Delete existing Hibernate XML mapping file
Stock.hbm.xml file එක මතකයිනේ? ඔව් ඔව්! mapping file එක තමයි බොලව්. ඔන්න ඔය "හු" යන්න delete කරපියව්! තව දුරටත් ඕකෙන් වැඩක් වෙන්නේ නෑ
5 - Update Hibernate configuration file.
දැං Update Hibernate configuration file එක ඔය පහල තියෙන විදියට edit කරගන්න ඕනේ.
මෙන්න මේ විදියට තමයි දැං Update Hibernate configuration file එක පේන්න තියෙන්නේ.
මේ? දැක්කද full project structure එක?
ඔන්න ඒකත් හරි!
Run කරලා බලන එකයි තියෙන්නේ.
App class උඩ right click කලා --> Run as --> Java application
පේනවනේ? මම අළුත් data ටිකක් දාගෙන තියෙන්නේ save කරගන්න.
Here is the result..
ඔය රතු පාටින් තියෙන්නේ මම අළුතෙන් දාපු data ටික
මේ වෙනස සැපයි කියලා හිතනවා උඹලට.
** එහෙම නැත්තන් උඹලට hbm.xml ලිය ලිය බලු කට්ටක් කන්න වෙන්නේ. දැං වැඩේ ලේසිනේ? Model class එකෙන්ම වැඩේ ගොඩ.
හරි! ඊළඟට තියෙන්නේ ටිකක් ලොකු පිම්මක් පනින්න. අපි Maven ගැන ඉගෙන ගන්න යන්නේ.
Maven Athapaththu??



නෑ නෑ!


මේක dependency managment and build automation tool එකක්.
ඒ උනාට Maven Athapaththu වගේම තාක්ෂණයෙන් බොහෝම අනූනයි.

ඔන්න දැන් අපි ටිකෙන් ටික more advanced technologies use කරන්න පටන් ගන්නවා.
ඉතිං basic හරියට නැති කට්ටිය basic brush-up කරගෙනම ඊළඟ lessons වලට join උනොත් තමයි හොඳ. 
මේකට උඹලගේ කැපවීම අනිවාර්යයි! මට පුලුවන් බොලාට මඟ පෙන්නන්න විතරයි!

එහෙනම් හැමෝටම ජය!
කලින් දාපු lessons ටික මෙතනින් බලල go through කරපල්ලා
Last edited:


