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

Email component of User

parent 4efe98b5
No related branches found
No related tags found
No related merge requests found
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
}
}
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
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
<?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>
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