楼主: Lisrelchen
1295 9

Java Hibernate Cookbook [推广有奖]

  • 0关注
  • 62粉丝

VIP

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50057 个
通用积分
79.6987
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. Author:Vishal Ranapariya, Yogesh Prajapati
  2. Isbn:1784391905
  3. Year:2015
  4. Pages:250
  5. Language:English
  6. File size:2.04 MB
  7. File format:PDF
  8. Category:Java
  9. Book Description:

  10. Hibernate is a database independent technology, so the same code will work for all databases. It helps a Java developer write a query by mapping Java bean to database tables and help create tuned queries that boost performance. Even with limited SQL knowledge one can easily perform database operations. This makes the development faster and more accurate than JDBC. Hibernate supports useful features like connection pooling, caching, and inheritance etc.

  11. This book will provide a useful hands-on guide to Hibernate to accomplish the development of a real-time Hibernate application.

  12. We will start with the basics of Hibernate, which include setting up Hibernate – the pre-requisites and multiple ways of configuring Hibernate using Java. We will then dive deep into the fundamentals of Hibernate such as SessionFactory, session, criteria, working with objects and criteria. This will help a developer have a better understanding of how Hibernate works and what needs to be done to run a Hibernate application. Moving on, we will learn how to work with annotations, associations and collections. In the final chapters, we will see explore querying, advanced Hibernate concepts and integration with other frameworks.
复制代码

本帖隐藏的内容

Java Hibernate Cookbook.pdf (2.02 MB)

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Hibernate Cookbook Book Cook Java Java

本帖被以下文库推荐

沙发
Lisrelchen 发表于 2016-8-26 08:35:11 |只看作者 |坛友微信交流群
  1. How to do it…
  2. In this section, we will take a look at how to insert, retrieve, delete, and update List,
  3. step by step.
  4. Inserting a record
  5. The following code is used to insert a record into the database. Here, we will try to insert the
  6. record of an employee with three e-mail addresses:
  7. Code
  8. SessionFactory sessionFactory =
  9. HibernateUtil.getSessionFactory();
  10. Session session = sessionFactory.openSession();
  11. Employee employee = new Employee();
  12. employee.setName("yogesh");
  13. List<String> emails = new ArrayList<String>();
  14. emails.add("emailaddress1@provider1.com");
  15. emails.add("emailaddress2@provider2.com");
  16. emails.add("emailaddress3@provider3.com");
  17. employee.setEmails(emails);
  18. session.getTransaction().begin();
  19. session.save(employee);
  20. session.getTransaction().commit();
复制代码

使用道具

藤椅
Lisrelchen 发表于 2016-8-26 08:36:39 |只看作者 |坛友微信交流群
  1. Retrieving a record
  2. Here, we know that the record is inserted with id 1. So, we will try to get only this record and
  3. understand how List works.
  4. Use the following code to retrieve the records of Employee#1:
  5. Code
  6. Employee employee = (Employee) session.get(Employee.class, 1l);
  7. System.out.println(employee.toString());
复制代码

使用道具

板凳
Lisrelchen 发表于 2016-8-26 08:38:04 |只看作者 |坛友微信交流群
  1. Updating a record
  2. Here, we will try to add one more e-mail address to the list of e-mail IDs for Employee#1,
  3. which means that we will update the list of e-mails. Use the following code to do so:
  4. Code
  5. Employee employee = (Employee) session.get(Employee.class, 1l);
  6. List<String> emails = employee.getEmails();
  7. emails.add("emailaddress3@provider3.com");
  8. session.getTransaction().begin();
  9. session.saveOrUpdate(employee);
  10. session.getTransaction().commit();
  11. System.out.println(employee.toString());
复制代码

使用道具

报纸
Lisrelchen 发表于 2016-8-26 08:39:21 |只看作者 |坛友微信交流群
  1. Deleting a record
  2. Here, we will try to delete the record of Employee#1 from the database using the following code:
  3. Code
  4. Employee employee = new Employee();
  5. employee.setId(1);
  6. session.getTransaction().begin();
  7. session.delete(employee);
  8. session.getTransaction().commit();
复制代码

使用道具

地板
ReneeBK 发表于 2016-11-28 01:37:10 |只看作者 |坛友微信交流群
  1. Getting the required libraries for hibernate

  2. To work with hibernate, we need a JAR (Java Archive) file provided by hibernate. Here, we will see how to download the hibernate core distribution. There are multiple ways to get the required libraries; here, we will consider two of them:

  3. Manually downloading
  4. Using Maven
  5. Manually downloading

  6. The first and most basic JAR file needed is a JDBC driver. The JDBC driver is a bridge or an API between Java and the database. The JDBC driver provides us with the generic classes that will help us communicate with the database. Generally, the driver is either provided by the database provider or developed by communities; however, you have to get it yourself. This also depends on the type of the database you are using. As we will use the MySQL database for this book, we will use the Mysql-Connector.jar file.

  7. How to do it…

  8. Let's come back to the library section. Apart from JDBC, you will need the JAR files for hibernate. Perform the following steps:

  9. Download the hibernate core distribution from http://hibernate.org/orm/.
  10. Now, place all the files in your classpath if you plan to run a standalone program and put them in the lib folder if it's a J2EE project.
复制代码

使用道具

7
ReneeBK 发表于 2016-11-28 01:37:34 |只看作者 |坛友微信交流群

Creating a hibernate persistent class

  1. Create the Employee.java class and place the following code in the class:
  2. public class Employee{
  3.   private long id;
  4.   private String firstName;
  5.   private double salary;
  6.   // other fields

  7.   // default constructor
  8.   public Employee() {
  9.   }

  10.   public long getId() {
  11.        return id;
  12.   }

  13.   public void setId(long id) {
  14.     this.id = id;
  15.   }

  16.   public String getFirstName() {
  17.     return firstName;
  18.   }

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

  22.   public double getSalary() {
  23.     return salary;
  24.   }

  25.   public void setSalary(double salary) {
  26.     this.salary = salary;
  27.   }
  28.   
  29.   //
  30.   // Getter and setter for other fields...
  31.   //

  32. }
复制代码
  1. The following code is the definition for the Department class in Department.java:

  2. public class Department{
  3.   private long id;
  4.   private String deptName;

  5.   //default constructor
  6.   public void Department(){
  7.   }

  8.   //getters and setters
  9.   public long getId() {
  10.     return id;
  11.   }

  12.   public void setId(long id) {
  13.     this.id = id;
  14.   }

  15.   public String getDeptName() {
  16.     return deptName;
  17.   }

  18.   public void setDeptName(String deptName) {
  19.     this.deptName = deptName;
  20.   }

  21. }
复制代码
  1. The following code is the definition for the Employee class in Employee.java:

  2. public class Employee{
  3.   private long id;
  4.   private String firstName;
  5.   private double salary;
  6.   private Department department; // reference to Department.

  7.   //default constructor
  8.   public void Employee(){
  9.   }

  10.   //getters and setters
  11.   public long getId() {
  12.     return id;
  13.   }

  14.   public void setId(long id) {
  15.     this.id = id;
  16.   }

  17.   public String getFirstName() {
  18.     return firstName;
  19.   }

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

  23.   public double getSalary() {
  24.     return salary;
  25.   }

  26.   public void setSalary(double salary) {
  27.     this.salary = salary;
  28.   }

  29.   public Department getDepartment(){
  30.     return department;
  31.   }

  32.   public setDepartment(Department department){
  33.     this.department = department;
  34.   }
  35.   
  36. }
复制代码

使用道具

8
ReneeBK 发表于 2016-11-28 01:40:46 |只看作者 |坛友微信交流群

Providing an XML-based hibernate mapping

  1. How to do it…

  2. To provide hibernate mapping based on XML, perform the following steps:

  3. Ensure that the basic structure of the configuration file is as follows:
  4. <!DOCTYPE hibernate-mapping PUBLIC
  5. "-//Hibernate/Hibernate Mapping DTD X.X//EN"
  6. "http://hibernate.sourceforge.net/hibernate-mapping-X.X.dtd">
  7. <hibernate-mapping>
  8. ...
  9. </hibernate-mapping>
  10. Note

  11. We have not provided the DTD code for the future demo in this book, but it should be present while developing.

  12. Create a XML file and name it Employee.hbm.xml. Then, add the configuration, as shown in the following code:
  13. <hibernate-mapping>
  14.   <class="Employee" table="employee">
  15.     <id name="id" type="long" column="id">
  16.       <generator class="increment" />
  17.     </id>
  18.     <property column="firstName" name="firstName" />
  19.     <property column="salary" name="salary" />
  20.     <!-- other properties mapping-->
  21.   </class>
  22. </hibernate-mapping>
复制代码

使用道具

9
ReneeBK 发表于 2016-11-28 01:42:07 |只看作者 |坛友微信交流群

Providing an annotation-based hibernate mapping

  1. How to do it…

  2. Now, let's create the class that contains the annotation-based mapping. As we used the Employee class to provide XML-based mapping here, we will use the same class with annotations:

  3. Represent the annotation-based mapping for the Employee class in Employee.java, as shown in the following code:
  4. import javax.persistence.Column;
  5. import javax.persistence.Entity;
  6. import javax.persistence.GeneratedValue;
  7. import javax.persistence.Id;
  8. import javax.persistence.Table;

  9. @Entity
  10. @Table(name="employee")
  11. public class Employee{

  12.   @Id
  13.   @Column(name="id")
  14.   @GeneratedValue(strategy = GenerationType.AUTO)
  15.   private long id;

  16.   @Column(name="firstname")
  17.   private String firstName;

  18.   @Column(name = "salary")
  19.   private double salary;

  20.   // default constructor
  21.   public Employee() {
  22.   }

  23.   public long getId() {
  24.     return id;
  25.   }

  26.   public void setId(long id) {
  27.     this.id = id;
  28.   }

  29.   public String getFirstName() {
  30.       return firstName;
  31.   }

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

  35.   public double getSalary() {
  36.     return salary;
  37.   }

  38.   public void setSalary(double salary) {
  39.     this.salary = salary;
  40.   }

  41. }
复制代码

使用道具

10
southernrude 发表于 2017-11-20 21:00:18 来自手机 |只看作者 |坛友微信交流群
Lisrelchen 发表于 2016-8-26 07:18
**** 本内容被作者隐藏 ****
谢谢分享

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-15 01:50