|
| 1 | +title: Java: Hibernate p1: with SQLite |
| 2 | +date: 2012-03-26 18:30:13 |
| 3 | +tags: java,java-hibernate |
| 4 | + |
| 5 | +First hibernate needs a small army of jars. You need: |
| 6 | + |
| 7 | +* hibernatesqlite |
| 8 | +* hibernate-entitymanager |
| 9 | +* sqlite-jdbc |
| 10 | + |
| 11 | +Now create a sqlite3 database in your current directory. Give it an id for the primary key and a text name column. Call them 'id' and 'name'. Name the table employee. Name the file dby.db. |
| 12 | + |
| 13 | +Then in src/main/resources create your hibernate.cfg.xml file. This states you're going to use a sqlite db, your using it the reference to the db your previously created, and your specifying a mapping that we'll get to next. |
| 14 | + |
| 15 | + <?xml version="1.0" encoding="utf-8"?> |
| 16 | + <!DOCTYPE hibernate-configuration SYSTEM |
| 17 | + "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> |
| 18 | + |
| 19 | + <hibernate-configuration> |
| 20 | + <session-factory> |
| 21 | + |
| 22 | + <property name="hibernate.dialect"> |
| 23 | + com.applerao.hibernatesqlite.dialect.SQLiteDialect |
| 24 | + </property> |
| 25 | + <property name="hibernate.connection.driver_class"> |
| 26 | + org.sqlite.JDBC |
| 27 | + </property> |
| 28 | + <property name="hibernate.connection.url"> |
| 29 | + jdbc:sqlite:dby.db |
| 30 | + </property> |
| 31 | + |
| 32 | + <mapping resource="Employee.hbm.xml"/> |
| 33 | + |
| 34 | + </session-factory> |
| 35 | + </hibernate-configuration> |
| 36 | + |
| 37 | +In the same directory we're going to create that mapping between your DB and a java class. |
| 38 | + |
| 39 | + <?xml version="1.0" encoding="UTF-8"?> |
| 40 | + <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" |
| 41 | + "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> |
| 42 | + <hibernate-mapping> |
| 43 | + <class name="model.Employee" table="employee"> |
| 44 | + <id name="id" column="id" type="long"> |
| 45 | + <generator class="native"></generator> |
| 46 | + </id> |
| 47 | + <property name="name" column="name" type="string"></property> |
| 48 | + </class> |
| 49 | + </hibernate-mapping> |
| 50 | + |
| 51 | +It's saying we have a class called employee in Employee, which relates to the employee take in the database. It say it has both a id and a name property, which are of the type long and string respectively. The id tag uses the generator native to say make it autoincrement. |
| 52 | + |
| 53 | +Now we need to actually create that class, src/main/java/model/Employee.java. It a Plain Old Java Object. |
| 54 | + |
| 55 | + package model; |
| 56 | + |
| 57 | + public class Employee { |
| 58 | + |
| 59 | + private long id = 1L; |
| 60 | + |
| 61 | + private String name; |
| 62 | + |
| 63 | + public Employee() { |
| 64 | + } |
| 65 | + |
| 66 | + public Employee(String fname) { |
| 67 | + name = fname; |
| 68 | + } |
| 69 | + |
| 70 | + public long getId() { |
| 71 | + return id; |
| 72 | + } |
| 73 | + |
| 74 | + public void setId(Long id) { |
| 75 | + this.id = id; |
| 76 | + } |
| 77 | + |
| 78 | + public String getName() { |
| 79 | + return name; |
| 80 | + } |
| 81 | + |
| 82 | + public void setName(String name) { |
| 83 | + this.name = name; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | +Now we need to create our class that actually deals with that. We first create a SessionFactory. This allows us to make session requests that actually talk to the DB: |
| 88 | + |
| 89 | + try{ |
| 90 | + mFctory = new Configuration().configure().buildSessionFactory(); |
| 91 | + }catch (Throwable ex) { |
| 92 | + System.err.println("Couldn't create session factory." + ex); |
| 93 | + throw new ExceptionInInitializerError(ex); |
| 94 | + } |
| 95 | + |
| 96 | +Now we have that we can use it to save an Employee we make. |
| 97 | + |
| 98 | + Session session = mFactory.openSession(); |
| 99 | + Transaction tx = null; |
| 100 | + Long employeeID = null; |
| 101 | + try{ |
| 102 | + tx = session.beginTransaction(); |
| 103 | + Employee employee = new Employee(fname); |
| 104 | + employeeID = (Long) session.save(employee); |
| 105 | + tx.commit(); |
| 106 | + }catch (HibernateException e) { |
| 107 | + if (tx!=null) tx.rollback(); |
| 108 | + e.printStackTrace(); |
| 109 | + }finally { |
| 110 | + session.close(); |
| 111 | + } |
| 112 | + |
| 113 | +We first get open a session via the factory, then begin a transaction, save your employee to that session, and commit it. The Employee should not be saved to the database. |
| 114 | + |
| 115 | +Getting objects from the database is very much the same: |
| 116 | + |
| 117 | + List employees = session.createQuery("FROM Employee").list(); |
| 118 | + |
| 119 | +We're create a query with HQL that gets everything from 'Employee'. |
| 120 | + |
| 121 | +Updating a query is a matter of getting the object from the session variable, and then setting that to update: |
| 122 | + |
| 123 | + Employee employee = (Employee)session.get(Employee.class, itsID); |
| 124 | + session.update(employee); |
| 125 | + |
| 126 | +Deleting is the same, except you run session.delete() in place of session.update(). |
0 commit comments