Skip to content
Snippets Groups Projects
Commit 2efab615 authored by Goik Martin's avatar Goik Martin
Browse files

Two component addresses having appropriate naming strategy

parent 741b1558
No related branches found
No related tags found
No related merge requests found
......@@ -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>
......
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
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
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
<?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>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment