diff --git a/ws/eclipse/HibIntro/src/main/java/component/address1/Email.java b/ws/eclipse/HibIntro/src/main/java/component/address1/Email.java new file mode 100644 index 0000000000000000000000000000000000000000..5b1aef42c96beea7ae1e93379121c7f15a176f0d --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/component/address1/Email.java @@ -0,0 +1,19 @@ +package component.address1; + +import javax.persistence.Embeddable; + +@Embeddable +public class Email { + + private String emailAddress; + public String getEmailAddress() { return emailAddress;} + public void setEmailAddress(final String emailAddress) { this.emailAddress = emailAddress;} + + protected Email() {} + public Email(final String emailAddress) { + setEmailAddress(emailAddress); + } + void sendEmail(final String subject, final String content) { + //Not yet implemented + } +} diff --git a/ws/eclipse/HibIntro/src/main/java/component/address1/PersistUser.java b/ws/eclipse/HibIntro/src/main/java/component/address1/PersistUser.java new file mode 100644 index 0000000000000000000000000000000000000000..330094061b4365d24715fbda89e79e8844e4353d --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/component/address1/PersistUser.java @@ -0,0 +1,30 @@ +package component.address1; + +import hibintro.util.HibernateUtil; + +import org.hibernate.Session; +import org.hibernate.Transaction; + +/** + * @author goik + * + * Persisting a simple instance of {@link User} + * + */ +public class PersistUser { + + /** + * @param args not used. + */ + public static void main(String[] args) { + final Session session = HibernateUtil.createSessionFactory("component/address1/hibernate.cfg.xml").openSession(); + + final Transaction transaction = session.beginTransaction(); + { + final User u = new User(123, "goik", "Martin Goik"); + u.setEmailAddress(new Email("goik@hdm-stuttgart.de")); + session.save(u); + } + transaction.commit(); + } +} \ No newline at end of file diff --git a/ws/eclipse/HibIntro/src/main/java/component/address1/User.java b/ws/eclipse/HibIntro/src/main/java/component/address1/User.java new file mode 100644 index 0000000000000000000000000000000000000000..0d4d704b04dd3ed2d32ef6de4fca49de42df2701 --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/component/address1/User.java @@ -0,0 +1,88 @@ +package component.address1; + +import javax.persistence.Column; +import javax.persistence.Embedded; +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 addresses + * + */ +@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;} + + private Email address; + @Embedded + public Email getEmailAddress() { return address;} + public void setEmailAddress(final Email address) { this.address = address;} + + protected User() {} + + /** + * @param uidNumber See {@link #getUidNumber()}. + * @param uid See {@link #getUid()}. This parameter must not be null + * @param cname See {@link #getCname()}. + */ + public User(final int uidNumber, final String uid, final String cname) { + setUidNumber(uidNumber); + setUid(uid); + setCname(cname); + } + @Override + public boolean equals(Object other) { + if (this == other) { + return true; + } else if (getUid() == null) { + return false; + } else if (other instanceof User) { + final User that = (User) other; + return this.getUid().equals( that.getUid() ); + } else { + return false; + } + } + @Override + public int hashCode() { + if (null == getUid()) { + return System.identityHashCode(this); + } else { + return getUid().hashCode(); + } + } +} \ No newline at end of file diff --git a/ws/eclipse/HibIntro/src/main/java/component/address1/hibernate.cfg.xml b/ws/eclipse/HibIntro/src/main/java/component/address1/hibernate.cfg.xml new file mode 100644 index 0000000000000000000000000000000000000000..aa6503aef36c84cc5dea532284cf18710000edfc --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/component/address1/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">true</property> + <property name="hibernate.format_sql">true</property> + <property name="hibernate.hbm2ddl.auto">update</property> + + <mapping class="component.address1.User"/> + </session-factory> +</hibernate-configuration>