java web development 9 කොටස - Hibernate with annotations

J Sparrow

Well-known member
  • Jan 8, 2012
    1,603
    698
    113
    127.0.0.1
    හා හා! දැං තියෙන වැඩ පැත්තකින් දාලා වරෙව්!

    STS open කරගෙන, MySQL open කරගෙන, database connection හදාගෙන ලෑස්ති වෙයව්! :angry::angry::angry:

    හොඳායි....

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

    Annotation එකක් කියන්නේ මොකද්ද කියලා කට්ටිය දන්නවා ඇති කියලා හිතනවා.

    O8uGm9H.png


    ඔය නිල් පාටින් කොටු කරලා තියෙන්නේ Annotations . පොඩි හරි idea එකක් ගන්න පුළුවන්නේ? කොහොමද මේක use වෙන්නේ කියලා?

    මේකට අපි කලින් හදපු project එකම use කරනවා මම. බොලාලට ඉතිං කැමැත්තක් කොරන්ඩ පුලුවනි! අළුතෙන්ම එකක් හදනවානම් තමයි ඉතිං හොඳ. :yes:

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

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

    1 - Add Hibernate annotation dependency.

    කලින් පාවිච්චි කරපු dependencies වලට අමතරව අපිට තව මේ dependency දෙකත් add කරගන්න වෙනවා!

    i - hibernate-annotations --> 3.3.0.GA
    ii - hibernate-commons-annotations --> 3.3.0.GA

    ඔය? පේනවනේ?


    J7ncn0f.png


    ඔන්න මම add කරගත්තා.

    2 - Update HibernateUtil.java

    මතකයිනේ? එදා අපි හදා ගත්තා HibernateUtil කියලා class එකක්?

    Rpzsk7M.png


    ඔව් ඔව්! ඔය තියෙන්නේ... ;)

    ඒක 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 කරගන්න ඕනේ.

    STiRpVR.png


    මෙන්න මේ විදියට තමයි දැං Update Hibernate configuration file එක පේන්න තියෙන්නේ.

    nQ26xr0.png


    මේ? දැක්කද full project structure එක?

    d2aengt.png


    ඔන්න ඒකත් හරි!

    Run කරලා බලන එකයි තියෙන්නේ.

    App class උඩ right click කලා --> Run as --> Java application


    L4f1gz2.png


    පේනවනේ? මම අළුත් data ටිකක් දාගෙන තියෙන්නේ save කරගන්න.

    Here is the result..

    qc85jgy.png


    ඔය රතු පාටින් තියෙන්නේ මම අළුතෙන් දාපු data ටික

    මේ වෙනස සැපයි කියලා හිතනවා උඹලට.
    ** එහෙම නැත්තන් උඹලට hbm.xml ලිය ලිය බලු කට්ටක් කන්න වෙන්නේ. දැං වැඩේ ලේසිනේ? Model class එකෙන්ම වැඩේ ගොඩ.

    හරි! ඊළඟට තියෙන්නේ ටිකක් ලොකු පිම්මක් පනින්න. අපි Maven ගැන ඉගෙන ගන්න යන්නේ.

    Maven Athapaththu?? :rofl::rofl::rofl:

    194497.4.jpg


    නෑ නෑ! :angry::angry:

    මේක dependency managment and build automation tool එකක්.

    ඒ උනාට Maven Athapaththu වගේම තාක්ෂණයෙන් බොහෝම අනූනයි. :yes:

    ඔන්න දැන් අපි ටිකෙන් ටික more advanced technologies use කරන්න පටන් ගන්නවා. :cool: ඉතිං basic හරියට නැති කට්ටිය basic brush-up කරගෙනම ඊළඟ lessons වලට join උනොත් තමයි හොඳ. ;)

    මේකට උඹලගේ කැපවීම අනිවාර්යයි! මට පුලුවන් බොලාට මඟ පෙන්නන්න විතරයි! :yes:

    එහෙනම් හැමෝටම ජය!


    කලින් දාපු lessons ටික මෙතනින් බලල go through කරපල්ලා
     
    Last edited:

    GT9

    Well-known member
  • Jun 22, 2013
    1,564
    167
    63
    [email protected]
    මචං ඔයා PHP පැත්තත් දන්නව නම් JAVA වලින් WEB DEVELOPMENT කරන එක PHP වගේ ලෑන්ග් එකකට වඩා ලේසිද? හොඳද?
     

    J Sparrow

    Well-known member
  • Jan 8, 2012
    1,603
    698
    113
    127.0.0.1
    මචං ඔයා PHP පැත්තත් දන්නව නම් JAVA වලින් WEB DEVELOPMENT කරන එක PHP වගේ ලෑන්ග් එකකට වඩා ලේසිද? හොඳද?

    PHP වලට වඩා ලේසිද කියන එක depends මචන්! but PHP වලට වඩා 100% හොඳයි!
    :yes:
     
    • Like
    Reactions: GT9

    KRipTER

    Well-known member
  • Sep 13, 2015
    3,243
    357
    83
    Everywhere...
    You must spread some Reputation around before giving it to J Sparrow again.
    :sorry::sorry::sorry::sorry::sorry::sorry::sorry:

    තෑන්ක්ස් මචං...:D:D:D

    Waiting for next lesson...:yes::yes::yes::yes:
     

    robaiya

    Member
    Feb 10, 2015
    1,869
    501
    0
    අහම්බෙන් වගේ මේක දැක්කේ, නියමයි අනිත් කොටසත් දාමු ඉක්මනට මේක බලන අය බම්ප් එකක් දා ගෙන යන්න.
    You must spread some Reputation around before giving it to J Sparrow again.
     
    Last edited:

    thedigit2s

    Well-known member
  • Oct 3, 2009
    2,751
    3,144
    113
    37
    @ katubedda
    ඇයි බ්‍රෝ php වලට වඩා ජාවා හොදයි කිවුවේ.... ටිකක් පැහැදිලි කරන්නකෝ. කලින් කියලා දීලා තියෙනවා නම් සමාවෙලා ලින්ක් එක එවන්න
     

    navodwickra

    Well-known member
  • Jan 17, 2007
    11,424
    2,140
    113
    Leeds, UK
    මචං ඔයා PHP පැත්තත් දන්නව නම් JAVA වලින් WEB DEVELOPMENT කරන එක PHP වගේ ලෑන්ග් එකකට වඩා ලේසිද? හොඳද?

    Web Development වලට නම් මචන් ගොඩක් companies Java use කරන්නේ නැහැ. Web development නම් උඹේ ආසාව PHP ඉගෙන ගනින් , නැත්නම් ඉතින් Node වගේ Server Side JS ඉගෙන ගනින්. එහෙමත් නම්ත්නම් ඉතින් .Net
     

    J Sparrow

    Well-known member
  • Jan 8, 2012
    1,603
    698
    113
    127.0.0.1
    අහම්බෙන් වගේ මේක දැක්කේ, නියමයි අනිත් කොටසත් දාමු ඉක්මනට මේක බලන අය බම්ප් එකක් දා ගෙන යන්න.
    You must spread some Reputation around before giving it to J Sparrow again.

    Rep nathuwata kamak na lokkaaa! ;)

    Onna kalin lessons tika. othanin go through karala balanna
    http://www.elakiri.com/forum/tags.php?tag=jsparrow
     

    J Sparrow

    Well-known member
  • Jan 8, 2012
    1,603
    698
    113
    127.0.0.1

    Web Development වලට නම් මචන් ගොඩක් companies Java use කරන්නේ නැහැ. Web development නම් උඹේ ආසාව PHP ඉගෙන ගනින් , නැත්නම් ඉතින් Node වගේ Server Side JS ඉගෙන ගනින්. එහෙමත් නම්ත්නම් ඉතින් .Net

    මෙහෙමයි, ඔය කතාවේ පැති දෙකක් තියෙනවා. PHP use කරලා ගොඩක් critical web applications develop කරන්න බෑ. security පැත්තෙන් මම දන්න තරමින් PHP ඉන්නේ java වලට වඩා ගොඩාක් පිටිපස්සෙන්. (*Correct me if I'm wrong.) Normal web site එකක් වගේ හදා ගන්නනම් ඉතිං PHP හොඳයි. උඹගේ ආසාව නිකන් web site develop කරන එකනම් PHP ඉගෙන ගනින්. :yes:
    Or come and experience the real deal. :cool:
    වැරදි තැන් තිබේනම් පෙන්වාදීමට සහ, සංවාදයට විවෘත්තයි!
    :love:

    Here is the proof.

    1 - One of the primary structural differences between PHP and Java is the difference between strongly- and weakly-typed languages. Java is a strongly-typed language, meaning it requires explicit statements of intent to function and that it is backed by a compiler.

    PHP, in contrast, is weakly typed, essentially meaning it is more flexible and reliant on “common sense programming” in how a task is accomplished.

    2 - Stability
    PHP has in my opinion, significant weaknesses. The procedural backward compatibility, no real deprecation mechanism, a mess semi platform independent libraries and functionality are just some of the issues the PHP. PHP lacks a clean cut, which the PHP planned to do with version 6.
    Java, however, has a clean platform independence and a fairly well-defined number of core libraries with appropriate quality standards.

    3 -
    Performance
    While Java was formerly often described as slow, today’s JVMs are highly optimized for speed, while the script languages, including PHP, still struggle with this. For example a first usable garbage collector will be shipped with PHP 5.3. Also other optimizations were moving very slowly into PHP Runtimes.

    4 - Integration
    Integration is certainly the strength of Java. On the one hand, Java itself is almost “Industry Standard”, on the other hand, there are many standards implementations in Java. If a PHP Web application should communicate with a specific protocol, the selection of libraries is rather limited. Even worse.

    Want more?

    Java itself is almost “Industry Standard” and companies choose PHP over java (Unless its a simple web application with no requirement for above facts) for web application development, are RETARDS! :cool:
     
    Last edited:

    J Sparrow

    Well-known member
  • Jan 8, 2012
    1,603
    698
    113
    127.0.0.1
    ඇයි බ්‍රෝ php වලට වඩා ජාවා හොදයි කිවුවේ.... ටිකක් පැහැදිලි කරන්නකෝ. කලින් කියලා දීලා තියෙනවා නම් සමාවෙලා ලින්ක් එක එවන්න

    කලින් කියලා දීලා නෑ බ්‍රෝ! උවමනාවක් තිබුනේ නෑ java is better than PHP කියන්න තරම්. ;)

    https://blog.codecentric.de/en/2008/07/comparison-of-java-and-php-for-web-applications/

    https://blog.udemy.com/php-vs-java/

    ඔන්න බලා ගන්න බ්‍රෝ! :yes:
     

    thedigit2s

    Well-known member
  • Oct 3, 2009
    2,751
    3,144
    113
    37
    @ katubedda
    Last edited: