Tuesday, 5 June 2012

Annotate non persistent properties with the @Transient Annotation

In Most of the cases, all the Java bean properties (example, firstname, lastname, email, etc.) that need to be stored to the database. But in some situation, we might have other properties that do not need to be stored in the database. In this case, we need to say to hibernate that do not take this property for all the Database operation. This can be done using @Transient annotation.

By default, all fields in your class will be persisted to the database if possible. If there is a field that should not be recorded in the database, mark that field with the @Transient annotation

Now let us do an example on the above subject

Step 1:

Create the hibernate setup as shown here;

Step 2:

Now let us modify our patient.java by adding a non persistent field

package mypack;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Transient;

@Entity
@Table(name = "patient")
public class patient {

    private Integer id;
    private String firstName;
    private String lastName;
    private String password;

    @Transient
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Id
    @GeneratedValue
    @Column(name = "ID")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "firstname")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name = "lastname")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

Step 3:

Let us modify test.java to create new patient as follows

package mypack;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.HibernateException;

import Utility.HibernateUtil;

public class Test {

private static SessionFactory factory;
private static ServiceRegistry serviceRegistry;

public static void main(String[] args) {
createpatient();
getAllRecords();
}

public static void getAllRecords() {
List allpatients;
Session session = HibernateUtil.beginTransaction();
Query q1 = session.createQuery("from patient");
allpatients = q1.list();
for (int i = 0; i < allpatients.size(); i++) {
patient pt = (patient) allpatients.get(i);
System.out.println(pt.getLastName());
}
HibernateUtil.CommitTransaction();
}

public static void createpatient() {

Session session = HibernateUtil.beginTransaction();
patient p1 = new patient();
p1.setFirstName("Mark");
p1.setLastName("Anderson");
p1.setPassword("1234");
session.save(p1);
HibernateUtil.CommitTransaction();
}

}


Step 4:


Run the test.java as Java Application and the following is the output


Hibernate: insert into patient (firstname, lastname) values (?, ?)
Hibernate: select patient0_.ID as ID0_, patient0_.firstname as firstname0_, patient0_.lastname as lastname0_ from patient patient0_
John
DAVID
JOSEPH
Anderson


Another good example


public enum Gender { MALE, FEMALE, UNKNOWN }

@Entity
public Person {
private Gender g;
private long id;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public long getId() { return id; }
public void setId(long id) { this.id = id; }

public Gender getGender() { return g; }
public void setGender(Gender g) { this.g = g; }

@Transient
public boolean isMale() {
return Gender.MALE.equals(g);
}

@Transient
public boolean isFemale() {
return Gender.FEMALE.equals(g);
}
}

94 comments:

  1. Thanks for sharing this informative blog. I have read your blog and I gathered some valuable information from this blog. Keep posting. Recently I did Digital Marketing courses in Chennai at a leading digital marketing company. It's really useful for me to make a bright career.

    ReplyDelete
    Replies
    1. I've read some just right stuff here. Certainly price bookmarking for revisiting. I surprise how a lot effort you put to make such a excellent informative website. study in australia consultant in jalandhar

      Delete
    2. The post is very informative. It is a pleasure reading it. I have also bookmarked you for checking out new posts. web designing jalandhar

      Delete
  2. Thanks for your informative article. With the world is totally dependent on internet, the future of digital marketing is on positive note. It also assures lucrative career opportunity for professionals looking for job in digital marketing. Digital Marketing Training in Chennai

    ReplyDelete
    Replies

    1. I have read your blog its very attractive and impressive. I like it your blog.

      Guaranteed SEO services Guaranteed SEO

      Delete
  3. Thanks for your informative article. Your post helped me a lot in understanding the career prospects in search engine optimization. Digital Marketing Training in Chennai | Digital Marketing Course in Chennai

    ReplyDelete
  4. Nice to learn something new from this article. You can refer some php training in chennai here.

    ReplyDelete
  5. SEO is the best method to gain traffic to a webpage. You have once again explained it very well here. This gives me an additional information in SEO campaign. Thanks for sharing this information in here, you are running a great blog. Keep up this good work.

    seo training institute in chennai | seo course in chennai | hadoop training

    ReplyDelete
  6. Your blog is very informative, thanks for sharing it.  E-Health Care Lists is one of the global suppliers of healthcare mailing list & email list. Marketing to the healthcare industry, reaching the doctors and other healthcare decision makers is often an impossible task. Healthcare Mailing List

    ReplyDelete
  7. There are lots of information about latest technology and how to get trained in them, like Best Hadoop Training In Chennai in Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies Hadoop Training in Chennai By the way you are running a great blog. Thanks for sharing this blogs..

    ReplyDelete
  8. I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    SalesForce Training in Chennai

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete
  10. This information is impressive..I am inspired with your post writing style & how continuously you describe this topic. After reading your post,thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..
    Android Training In Chennai In Chennai

    ReplyDelete
  11. SAP Training in Chennai
    This post is really nice and informative. The explanation given is really comprehensive and informative..

    ReplyDelete
  12. Oracle Training in chennai
    Thanks for sharing such a great information..Its really nice and informative..

    ReplyDelete
  13. Selenium Training in Chennai
    Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

    ReplyDelete
  14. Data warehousing Training in Chennai
    I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly..

    ReplyDelete
  15. Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly,
    but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing..
    Websphere Training in Chennai

    ReplyDelete
  16. This is really an awesome article. Thank you for sharing this.It is worth reading for everyone. Visit us:
    Oracle Training in Chennai

    ReplyDelete
  17. very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
    Oracle DBA Training in Chennai

    ReplyDelete
  18. great article!!!!!This is very importent information for us.I like all content and information.I have read it.You know more about this please visit again.
    Oracle RAC Training in Chennai

    ReplyDelete
  19. Wonderful tips, very helpful well explained. Your post is definitely incredible. I will refer this to my friend.
    SalesForce Training in Chennai

    ReplyDelete
  20. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
    Java Training in Chennai

    ReplyDelete
  21. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
    PHP Training in Chennai

    ReplyDelete
  22. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.Nice article i was really impressed by seeing this article, it was very interesting and it is very useful for me..
    Android Training in Chennai

    ReplyDelete
  23. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
    SAP Training in Chennai

    ReplyDelete
  24. Excellent information with unique content and it is very useful to know about the information based on blogs.
    Hadoop Training in Chennai

    ReplyDelete
  25. This was so useful and informative. The article helped me to learn something new.
    html course in chennai

    ReplyDelete
  26. This blog is Very close to my services Our database can help you to target prospects to get high response rates and guaranteed results. Now reach and engage with your targeted audience in the most modest way by opting Chiropodist Email List. Get the affordable Cardiologist Email Lists for sales leads. Healthcare professionals email list

    ReplyDelete

  27. I have read your blog, it was good to read & I am getting some useful info's through your blog keep sharing... Informatica is an ETL tools helps to transform your old business leads into new vision. Learn Informatica training in chennai from corporate professionals with very good experience in informatica tool.
    Regards,
    Best Informatica Training In Chennai|Informatica training center in Chennai

    ReplyDelete
  28. This technical post helps me to improve my skills set, thanks for this wonder article I expect your upcoming blog, so keep sharing.
    Regards,
    Informatica training chennai|Informatica course in Chennai

    ReplyDelete
  29. Oracle Training in chennai | Oracle D2K Training In chennai
    This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..

    ReplyDelete
  30. Thanks for Information Oracle Apps Technical is a collection of a bunch of collected applications like accounts payables, purchasing, inventory, accounts receivables, human resources, order management, general ledger and fixed assets, etc which have its own functionality for serving the business
    Oracle Apps Training In Chennai

    ReplyDelete
  31. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..
    Selenium Training in Chennai | QTP Training in Chennai

    ReplyDelete
  32. Digital Marketing are the best strategy to create brand awareness and promotion. Any business need this to popularize their product. Your content also provide the same information. Thanks for sharing such a wonderful information. Keep blogging.

    Digital Marketing Training in Chennai | Digital Marketing Course in Chennai | Digital Marketing Course

    ReplyDelete
  33. Web technology has evolved so much in the past years and it is being the part of a company's growth for a long time(Web designing course in chennai). It is explicitly printed in the above content. I really love the way you have started writing this article. It is the skill that every blogger require. Thanks for sharing this in here once again. Keep blogging like this(Web designing training).

    ReplyDelete
  34. Latest Govt Bank Jobs 2016


    This is awesome blog with smart content, Nice to see your post. Thanks......................

    ReplyDelete
  35. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
    Regards,
    sas training in Chennai|sas course in Chennai

    ReplyDelete
  36. The content of your blog is really interesting. I like this content and was very informative. If you are interested in taking SEO as your professional carrier visit this website.
    SEO Training in Chennai

    ReplyDelete
  37. Thanks for posting such a interesting blog and it is really informative. If you are interested in taking java as a professional carrier visit this website.JAVA Training in Chennai

    ReplyDelete
  38. Thanks for posting such a interesting blog. If you are interested in taking java as professional carrier visit this website.JAVA Training in Chennai|FITA Velachery

    ReplyDelete
  39. Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
    Oracle Training In Chennai

    ReplyDelete
  40. I am reading ur post from the beginning, it was so interesting to read & i feel thanks to you for posting such a good blog, keep updates regularly.Best Hadoop Training Institute In Chennai

    ReplyDelete
  41. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it. The python programming language is very popular and most widely used.
    Python Training in Chennai | Python Course in Chennai

    ReplyDelete
  42. Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
    msbi training in chennai

    ReplyDelete
  43. Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.
    hadoop training in chennai

    ReplyDelete
  44. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
    salesforce training in chennai | salesforce training institute in chennai

    ReplyDelete
  45. Thanks for sharing such a great information..Its really nice and informative..
    sas training in chennai

    ReplyDelete
  46. Good Post! Thank you so much for sharing...........
    websphere training in chennai

    ReplyDelete
  47. This comment has been removed by the author.

    ReplyDelete
  48. Excellent post!!!. The strategy you have posted in this technology helped me to get into the next level and had lot of information in it.
    Android training in Chennai | Android course in Chennai

    ReplyDelete
  49. Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

    iOS Training in Chennai

    ReplyDelete
  50. Thanks for sharing this with us it is a worth read. xcellent post!!! Our Digital Marketing Course is tailored for beginners who want to learn how to stand out digitally, whether it is for their own business or a personal brand.

    Digital Marketing Training in Chennai | Digital Marketing Course in Chennai | Digital Marketing Course | SEO Training in Chennai | Google Analytics Course | Social Media Marketing Course

    ReplyDelete
  51. Stations or telephone sets, sometimes called lines. Hikvision Security Wholesale

    ReplyDelete
  52. Shree Ram Techno Solutions Provides CCTV Camera, Security Camera, Wireless Security, Attendance System, Access Control System, DVR, NVR, Spy Camera, Fire Alarm, Security Alarm, PCI, IP Network Camera, Dome Camera, IR Camera, CCTV, Camera Price, HIKVISION, SCATI, Time Machine

    CCTV CAmera in jaipur at Rajasthan
    Home security system in jaipur
    Wireless Home Security System in jaipur
    Realtime attendance machine in jaipur
    cctv camera dealer in jaipur
    Hikvision DVR in jaipur at Rajasthan
    security system solutions in jaipur

    ReplyDelete
  53. Freelance Best Makeup & Hair Artist in Jaipur with huge experience and Specialization in Bridal and Wedding Makeup,Celebrity Makeup,Professional Makeup,Creative Makeup,Bollywood Makeup and Character Makeup in Delhi,Jaipur,Rajasthan. Natural Makeup that allows your skin to breath with a radiant glow and remains flawless throughout your special day.


    Best Makeup and Hairstyle in jaipur
    Fiza Makeup Academy in jaipur
    Best bridal makeup artist in jaipur(bollywood makeup,creative makeup,Airbrush makeup,character makeup)
    Make up and Hair kit
    Professional makeup artist course in jaipur
    Makeup and hairstyle tips
    Makeup and hair Images
    Makeup and hair tutorials
    Makeup and hair contract


    ReplyDelete
  54. Freelance Best Makeup & Hair Artist in Jaipur with huge experience and Specialization in Bridal and Wedding Makeup,Celebrity Makeup,Professional Makeup,Creative Makeup,Bollywood Makeup and Character Makeup in Delhi,Jaipur,Rajasthan. Natural Makeup that allows your skin to breath with a radiant glow and remains flawless throughout your special day.


    Best Makeup and Hairstyle in jaipur
    Fiza Makeup Academy in jaipur
    Best bridal makeup artist in jaipur(bollywood makeup,creative makeup,Airbrush makeup,character makeup)
    Make up and Hair kit
    Professional makeup artist course in jaipur
    Makeup and hairstyle tips
    Makeup and hair Images
    Makeup and hair tutorials
    Makeup and hair contract


    ReplyDelete
  55. Freelance Best Makeup & Hair Artist in Jaipur with huge experience and Specialization in Bridal and Wedding Makeup,Celebrity Makeup,Professional Makeup,Creative Makeup,Bollywood Makeup and Character Makeup in Delhi,Jaipur,Rajasthan. Natural Makeup that allows your skin to breath with a radiant glow and remains flawless throughout your special day.


    Best Makeup and Hairstyle in jaipur
    Fiza Makeup Academy in jaipur
    Best bridal makeup artist in jaipur(bollywood makeup,creative makeup,Airbrush makeup,character makeup)
    Make up and Hair kit
    Professional makeup artist course in jaipur
    Makeup and hairstyle tips
    Makeup and hair Images
    Makeup and hair tutorials
    Makeup and hair contract


    ReplyDelete
  56. Thanks for this post is very informative and interesting.all the points are very useful. Simple but very effective writing. Thanks for sharing such a nice post

    Bridal Makeup Chennai

    ReplyDelete
  57. A Pioneer Institute owned by industry professionals to impart vibrant, innovative and global education in the field of Hospitality to bridge the gap of 40 lakh job vacancies in the Hospitality sector. The Institute is contributing to the creation of knowledge and offer quality program to equip students with skills to face the global market concerted effort by dedicated faculties, providing best learning environment in fulfilling the ambition to become a Leading Institute in India.

    cha jaipur
    management college in jaipur
    management of hospitality administration jaipur
    cha management jaipurs
    Hotel management in jaipur
    Best hotel college in jaipur
    Best management college in jaipur
    College of Hospitality Administration, Jaipur
    Top 10 hotel management in jaipur
    Hotel managementin Rajasthan

    ReplyDelete
  58. Truly a very good article on how to handle the future technology. After reading your post,thanks for taking the time to discuss this, I feel happy about and I love learning more about this topic. Torusinfotech is one of the Bwst SEO Company in Delhi, India and low cost SEO service providers. Our low cost SEO service is a great way to start with our service and experience the change in your business...SEO Training Institute in Delhi

    ReplyDelete
  59. Our Digital Marketing Training is tailored for beginners who want to learn how to stand out digitally, whether it is for their own business or a personal brand.

    digital marketing course

    ReplyDelete
  60. This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information&its very useful to me...
    Android training in chennai
    Ios training in chennai

    ReplyDelete
  61. Nowadays some healthcare providers have begun to rely heavily on technology. healthcare mobile apps allows patients email and video chat with physicians and schedule appointments. They are not only convenient for patients but also for physicians.
    healthcare mobile app developer

    ReplyDelete
  62. That was a great message in my carrier, and It's wonderful commands like mind relaxes with  understand words of knowledge by information's.
    Websphere Training in Chennai

    ReplyDelete
  63. Oh! Wow! Nice informative post. I thank you for sharing the information about how to promote a website. Java advantage many uses for to all web application developers. This factors to promote client's website and own as well.
    Hadoop Training in Chennai
    Hadoop Training

    ReplyDelete

  64. Nice it seems to be good post... It will get readers engagement on the article since readers engagement plays an vital role in every blog.. i am expecting more updated posts from your hands.
    Android App Development Company

    ReplyDelete
  65. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete
  66. I am always searching online for tips that can benefit me. Thanks!


    www.digitalbrief.com

    ReplyDelete