diff --git a/ws/eclipse/HibIntro/src/main/java/session2/LoadUser.java b/ws/eclipse/HibIntro/src/main/java/session2/LoadUser.java new file mode 100644 index 0000000000000000000000000000000000000000..2aeec66210d143b6249d52fbf04185142383191d --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/session2/LoadUser.java @@ -0,0 +1,48 @@ +package session2; + + + +import hibintro.util.HibernateUtil; +import org.hibernate.Session; +import org.hibernate.Transaction; + +/** + * @author goik + * + * Repeated retrieval of a single {@link User} object from a database. + * + */ +public class LoadUser { + + /** + * Retrieving a single {@link User} instance by means + * of its primary key value two times in succession. + * @param args unused + */ + public static void main(String[] args) { + + final User user; + { + final Session session = HibernateUtil.createSessionFactory("session2/hibernate.cfg.xml").openSession(); + final Transaction transaction = session.beginTransaction(); + user = (User) session.load(User.class, 1L); + final User user1 = (User) session.load(User.class, 1L); + + System.out.println(" first transaction: user.equals(user1):" + user.equals(user1)); + + transaction.commit(); + session.close(); + } + { + final Session session = HibernateUtil.createSessionFactory("session2/hibernate.cfg.xml").openSession(); + final Transaction transaction = session.beginTransaction(); + final User user2 = (User) session.load(User.class, 1L); + + boolean val = user.equals(user2); + System.out.println("second transaction: user.equals(user2):" + val); + + transaction.commit(); + session.close(); + } + } +} \ No newline at end of file diff --git a/ws/eclipse/HibIntro/src/main/java/session2/PersistSingleUser.java b/ws/eclipse/HibIntro/src/main/java/session2/PersistSingleUser.java new file mode 100644 index 0000000000000000000000000000000000000000..8949901ad47f12a91191b365f0110a14b8cffa38 --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/session2/PersistSingleUser.java @@ -0,0 +1,29 @@ +package session2; + +import hibintro.util.HibernateUtil; + +import org.hibernate.Session; +import org.hibernate.Transaction; + +/** + * @author goik + * + * Persisting a simple instance of {@link User} + * + */ +public class PersistSingleUser { + + /** + * @param args not used. + */ + public static void main(String[] args) { + final Session session = HibernateUtil.createSessionFactory("session2/hibernate.cfg.xml").openSession(); + + final Transaction transaction = session.beginTransaction(); + { + final User u = new User(123, "goik", "Martin Goik"); + session.save(u); + } + transaction.commit(); + } +} \ No newline at end of file diff --git a/ws/eclipse/HibIntro/src/main/java/session2/User.java b/ws/eclipse/HibIntro/src/main/java/session2/User.java new file mode 100644 index 0000000000000000000000000000000000000000..1d7eb4281ca45f8e2b8211d9fc09543891bd7ae9 --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/session2/User.java @@ -0,0 +1,87 @@ +package session2; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.UniqueConstraint; + +/** + * @author goik + * + * {@link User} instances with primary key and unique constraint + * + */ +@Entity +@Table(uniqueConstraints={@UniqueConstraint(columnNames={"uid"}), + @UniqueConstraint(columnNames={"uidNumber"})}) +public class User { + + @Id + @GeneratedValue + private Long id; + public Long getId() {return id;} + protected void setId(Long id) {this.id = id;} + + int uidNumber; + public int getUidNumber() {return uidNumber;} + public void setUidNumber(int uidNumber) {this.uidNumber = uidNumber;} + + String uid; + + /** + * + * @return The user's unique login name e.g. "goik" + */ + @Column(nullable=false) + public String getUid() {return uid;} + public void setUid(String uid) {this.uid = uid;} + + String cname; + /** + * @return The user's common name e.g. "Martin Goik" + */ + @Column(nullable = false) + public String getCname() {return cname;} + public void setCname(String cname) {this.cname = cname;} + + /** + * Hibernate/JPA require a default constructor + */ + public User() {} + + /** + * @param uid + * See {@link #getUid()}. + * @param cname + * See {@link #getCname()}. + */ + public User(int uidNumber, String uid, String cname) { + setUidNumber(uidNumber); + setUid(uid); + setCname(cname); + } + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } else if (id == null) { + return false; + } else if (other instanceof User) { + final User that = (User) other; + return this.id.equals( that.getId() ); + } else { + return false; + } + } + @Override + public int hashCode() { + if (null == id) { + return System.identityHashCode(this); + } else { + return id.hashCode(); + } + } + +} \ No newline at end of file diff --git a/ws/eclipse/HibIntro/src/main/java/session2/hibernate.cfg.xml b/ws/eclipse/HibIntro/src/main/java/session2/hibernate.cfg.xml new file mode 100644 index 0000000000000000000000000000000000000000..c733be075fcc249d24bd4e3ac801dcad6be2f4e6 --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/session2/hibernate.cfg.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!DOCTYPE hibernate-configuration PUBLIC + "-//Hibernate/Hibernate Configuration DTD 3.0//EN" + "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> + +<hibernate-configuration> + <session-factory > + <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> + <property name="hibernate.connection.password">XYZ</property> + <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hdm</property> + <property name="hibernate.connection.username">hdmuser</property> + <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> + <property name="hibernate.show_sql">false</property> + <property name="hibernate.format_sql">true</property> + <property name="hibernate.hbm2ddl.auto">update</property> + + <mapping class="session2.User"/> + </session-factory> +</hibernate-configuration>