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

symmetric getter and setter methods

parent 14891191
No related branches found
No related tags found
No related merge requests found
......@@ -18909,6 +18909,44 @@ public class Laptop {
...
@OneToOne (<emphasis role="bold">mappedBy="laptop"</emphasis>)
public Employee getOwner() { return owner;} ...</programlisting>
<qandaset role="exercise">
<title>Symmetric implementation of getter and setter
methods</title>
<qandadiv>
<qandaentry>
<question>
<para>The current implementation is not symmetric with
respect to entity.company5.model.Employee.setLaptop and
entity.company5.model.Laptop.setOwner. Implement a better
solution allowing both variants:</para>
<programlisting>Employee e ...;
Laptop l ... ;
e.setLaptop(l);
...
l.setOwner(e);</programlisting>
</question>
</qandaentry>
</qandadiv>
</qandaset>
<qandaset role="exercise">
<title>Transactions and data integrity</title>
<qandadiv>
<qandaentry>
<question>
<para>Use your implementation to exchange the laptops of two
users. Which problem possibly arises? Is there a
solution?</para>
</question>
</qandaentry>
</qandadiv>
</qandaset>
</section>
</section>
</chapter>
......
......@@ -15,7 +15,7 @@
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="entity.company5.Laptop"/>
<mapping class="entity.company5.Employee"/>
<mapping class="entity.company5.model.Laptop"/>
<mapping class="entity.company5.model.Employee"/>
</session-factory>
</hibernate-configuration>
package entity.company6;
import hibintro.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import entity.company6.model.Employee;
import entity.company6.model.Laptop;
/**
* Removing {@link Laptop} from {@link Employee}
*/
public class ExchangeLaptops {
/**
* @param args not used.
*/
public static void main(String[] args) {
final Session session = HibernateUtil.createSessionFactory("entity/company6/hibernate.cfg.xml").openSession();
{
final Transaction transaction = session.beginTransaction();
final Employee martin = (Employee) session.load(Employee.class, 1L),
eve = (Employee) session.load(Employee.class, 2L);
final Laptop currentMartin2214 = (Laptop) session.load(Laptop.class, 1L),
currentEve2213 = (Laptop) session.load(Laptop.class, 2L);
eve.setLaptop(currentMartin2214);
currentEve2213.setOwner(martin);
System.out.println("Martin:" + martin.getLaptop().getId());
System.out.println("Eve:" + eve.getLaptop().getId());
System.out.println("currentEve2213:" + currentEve2213.getOwner().getCname());
System.out.println("currentMartin2214:" + currentMartin2214.getOwner().getCname());
transaction.commit();
}
}
}
\ No newline at end of file
package entity.company6;
import hibintro.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import entity.company6.model.Employee;
import entity.company6.model.Laptop;
public class Persist {
public static void main(String[] args) {
final Session session = HibernateUtil.createSessionFactory("entity/company6/hibernate.cfg.xml").openSession();
{
final Transaction transaction = session.beginTransaction();
final Employee martin = new Employee(123, "Martin Goik"),
eve = new Employee(140, "Eve Blix");
final Laptop superCoolGadget = new Laptop(2213),
g2 = new Laptop(2214),
g3 = new Laptop(2215); // Not being referenced later
martin.setLaptop(g2);
eve.setLaptop(superCoolGadget);
// Now only save the employee transients,
// superCoolGadget will be saved automatically due to
// CascadeType.ALL
session.save(martin);
session.save(eve);
session.save(g3);
transaction.commit();
}
}
}
\ 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="entity.company6.model.Laptop"/>
<mapping class="entity.company6.model.Employee"/>
</session-factory>
</hibernate-configuration>
package entity.company6.model;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
/**
*
* {@link Employee} instances related to {@link Department}
*
*/
@Entity
@Table(uniqueConstraints={@UniqueConstraint(columnNames={"staffNumber"})})
public class Employee {
@Id
@GeneratedValue
public Long getId() {return id;}
protected void setId(Long id) {this.id = id;}
private Long id;
public int getStaffNumber() { return staffNumber;}
public void setStaffNumber(int staffNumber) { this.staffNumber = staffNumber;}
int staffNumber = Integer.MIN_VALUE;
public String getCname() { return cname;}
public void setCname(String cname) { this.cname = cname;}
private String cname; // Common name
/**
* One {@link Laptop} per {@link Employee}.
*
* @return {@link Laptop} "owned" by {@link Employee}.
*/
@OneToOne(optional=false, cascade={CascadeType.ALL})
public Laptop getLaptop() { return laptop;}
public void setLaptop(final Laptop laptop) {
if (getLaptop() == laptop) {
return;
}
if (null != getLaptop()) {
getLaptop().setOwnerInternal(null);
}
setLaptopInternal(laptop);
if (null != getLaptop()) {
getLaptop().setOwnerInternal(this);
}
}
protected void setLaptopInternal(final Laptop laptop) {
this.laptop = laptop;
}
private Laptop laptop;// Workstation for employees.
protected Employee(){}
public Employee(final int staffNumber, final String cname) {
setStaffNumber(staffNumber);
setCname(cname);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (Integer.MIN_VALUE == getStaffNumber()) {
return false;
} else if (other instanceof Employee) {
final Employee that = (Employee) other;
return this.getStaffNumber() == that.getStaffNumber();
} else {
return false;
}
}
@Override
public int hashCode() {
if (Integer.MIN_VALUE == getStaffNumber()) {
return System.identityHashCode(this);
} else {
return getStaffNumber();
}
}
}
\ No newline at end of file
package entity.company6.model;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
/**
*
* Laptops possibly being assigned to {@link Employee}
*
*/
@Entity
@Table(uniqueConstraints={@UniqueConstraint(columnNames={"idNumber"})})
public class Laptop {
@Id
@GeneratedValue
public Long getId() {return id;}
protected void setId(Long id) {this.id = id;}
private Long id;
/**
* @return The device's unique part number
*/
public int getIdNumber() { return idNumber;}
public void setIdNumber(int idNumber) { this.idNumber = idNumber;}
int idNumber;
@OneToOne (mappedBy="laptop",cascade={CascadeType.ALL})
public Employee getOwner() { return owner;}
public void setOwner(Employee owner) {
if (getOwner() == owner) {
return;
}
if (null != getOwner()) {
getOwner().setLaptopInternal(null);
}
setOwnerInternal(owner);
if (null != getOwner()) {
getOwner().setLaptopInternal(this);
}
}
protected void setOwnerInternal(Employee owner) {
this.owner = owner;
}
Employee owner;
protected Laptop(){}
public Laptop(final int idNumber) {
setIdNumber(idNumber);
}
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
} else if (Integer.MIN_VALUE == getIdNumber()) {
return false;
} else if (other instanceof Laptop) {
final Laptop that = (Laptop) other;
return this.getIdNumber() == that.getIdNumber();
} else {
return false;
}
}
@Override
public int hashCode() {
if (Integer.MIN_VALUE == getIdNumber()) {
return System.identityHashCode(this);
} else {
return getIdNumber();
}
}
}
\ No newline at end of file
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