diff --git a/Doc/course.xml b/Doc/course.xml index 21e2c700d5c21b60959a7720a4b6b572255aa868..9fd3d90946d305297110cf346e8bd560bf49e586 100644 --- a/Doc/course.xml +++ b/Doc/course.xml @@ -18111,30 +18111,30 @@ public class User { <glosslist> <glossentry> - <glossterm>Entity type</glossterm> + <glossterm>Value type</glossterm> <glossdef> - <para>Objects of this type do have their own database identity - and may exist independently of other (database) - entities.</para> + <para>An object of value type has no database identity. It + will appear in a database as a composite of a parent entity + type. Its life cycle is completely dependent on its + parent.</para> </glossdef> </glossentry> <glossentry> - <glossterm>Value type</glossterm> + <glossterm>Entity type</glossterm> <glossdef> - <para>An object of value type has no database identity. It - will appear in a database as a composite of a parent entity - type. Its lifecycle is completely dependent on its - parent.</para> + <para>Objects of this type do have their own database identity + and may exist independently of other (database) + entities.</para> </glossdef> </glossentry> </glosslist> </section> - <section xml:id="sect_MappingEmbeddedClass"> - <title>Mapping a single embedded class</title> + <section xml:id="sect_MappingComponents"> + <title>Mapping components</title> <para/> </section> diff --git a/ws/eclipse/HibIntro/src/main/java/component/address/Address.java b/ws/eclipse/HibIntro/src/main/java/component/address/Address.java new file mode 100644 index 0000000000000000000000000000000000000000..7d7a83d3d8ae8e462ced3dd2d6dbf196fa79d68c --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/component/address/Address.java @@ -0,0 +1,27 @@ +package component.address; + +import javax.persistence.Embeddable; + +@Embeddable +public class Address { + + private String street; + public String getStreet() { return street;} + public void setStreet(String street) { this.street = street;} + + private String city; + public String getCity() { return city;} + public void setCity(String city) { this.city = city;} + + private String zipcode; + public String getZipcode() { return zipcode;} + public void setZipcode(String zipcode) { this.zipcode = zipcode;} + + protected Address() {} + + public Address(final String street, final String city, final String zipcode) { + setStreet(street); + setCity(city); + setZipcode(zipcode); + } +} \ No newline at end of file diff --git a/ws/eclipse/HibIntro/src/main/java/component/address/PersistUser.java b/ws/eclipse/HibIntro/src/main/java/component/address/PersistUser.java new file mode 100644 index 0000000000000000000000000000000000000000..07996c61671a2d89e7881c88bd4f0c9ffdaf4c51 --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/component/address/PersistUser.java @@ -0,0 +1,33 @@ +package component.address; + +import hibintro.util.HibernateUtil; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.cfg.DefaultComponentSafeNamingStrategy; + +/** + * @author goik + * + * Persisting {@link User} instances having component E-Mail Addresses + * + */ +public class PersistUser { + + /** + * @param args not used. + */ + public static void main(String[] args) { + HibernateUtil.configuration.setNamingStrategy(new DefaultComponentSafeNamingStrategy()); + final Session session = HibernateUtil.createSessionFactory("component/address/hibernate.cfg.xml").openSession(); + + final Transaction transaction = session.beginTransaction(); + { + final User u = new User(123, "goik", "Martin Goik"); + u.setHomeAddress(new Address("144 Kings Highway", "S.W. Dover", "DE 19901")); + u.setWorkAddress(new Address("22 Baker St", "Morton", "PA 19070")); + session.save(u); + } + transaction.commit(); + } +} \ No newline at end of file diff --git a/ws/eclipse/HibIntro/src/main/java/component/address/User.java b/ws/eclipse/HibIntro/src/main/java/component/address/User.java new file mode 100644 index 0000000000000000000000000000000000000000..b61b4b432fd7a5e4e83c5522e653a1d44002a16f --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/component/address/User.java @@ -0,0 +1,93 @@ +package component.address; + +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 Address workAddress; + @Embedded + public Address getWorkAddress() { return workAddress;} + public void setWorkAddress(final Address workAddress) { this.workAddress = workAddress;} + + private Address homeAddress; + @Embedded + public Address getHomeAddress() { return homeAddress;} + public void setHomeAddress(Address homeAddress) { this.homeAddress = homeAddress;} + + 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/address/hibernate.cfg.xml b/ws/eclipse/HibIntro/src/main/java/component/address/hibernate.cfg.xml new file mode 100644 index 0000000000000000000000000000000000000000..0fc78c0afbacb6628889df63259ed8aa2d91b25a --- /dev/null +++ b/ws/eclipse/HibIntro/src/main/java/component/address/hibernate.cfg.xml @@ -0,0 +1,22 @@ +<?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> + <property name="hibernate.ejb.naming_strategy">DefaultComponentSafeNamingStrategy</property> + + + <mapping class="component.address.User"/> + </session-factory> +</hibernate-configuration>