[jboss-cvs] JBossAS SVN: r105004 - in projects/ejb-book/trunk: chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chxx and 7 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed May 19 18:20:39 EDT 2010
Author: ALRubinger
Date: 2010-05-19 18:20:38 -0400 (Wed, 19 May 2010)
New Revision: 105004
Added:
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chxx/
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chxx/entitymanager/
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chxx/entitymanager/Employee.java
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmbeddedEmployeePK.java
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeType.java
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithEmbeddedPK.java
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithExternalCompositePK.java
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithMappedSuperClassId.java
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithProperties.java
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/ExternalEmployeePK.java
projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/entity/AutogenIdentityBase.java
Removed:
projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/entity/Employee.java
projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java
Modified:
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/entity/IdentityBase.java
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/txwrap/TxWrappingBean.java
projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/txwrap/TxWrappingLocalBusiness.java
Log:
[EJBBOOK-27] Add examples to show Entity Mapping metadata
Copied: projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chxx/entitymanager/Employee.java (from rev 104997, projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/entity/Employee.java)
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chxx/entitymanager/Employee.java (rev 0)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chxx/entitymanager/Employee.java 2010-05-19 22:20:38 UTC (rev 105004)
@@ -0,0 +1,123 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.employeeregistry.chxx.entitymanager;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+/**
+ * Represents an Employee in the system. Modeled as a simple
+ * value object with some additional EJB and JPA annotations.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Entity
+// Mark that we're an Entity Bean, EJB's integration point
+// with Java Persistence
+public class Employee
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Primary key of this entity
+ */
+ @Id
+ // Mark that this field is the primary key
+ private Long id;
+
+ /**
+ * Name of the employee
+ */
+ private String name;
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Default constructor, required by JPA
+ */
+ public Employee()
+ {
+
+ }
+
+ /**
+ * Convenience constructor
+ */
+ public Employee(final long id, final String name)
+ {
+ // Set
+ this.id = id;
+ this.name = name;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Accessors / Mutators ---------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * @return the id
+ */
+ public Long getId()
+ {
+ return id;
+ }
+
+ /**
+ * @param id the id to set
+ */
+ public void setId(final Long id)
+ {
+ this.id = id;
+ }
+
+ /**
+ * @return the name
+ */
+ public String getName()
+ {
+ return name;
+ }
+
+ /**
+ * @param name the name to set
+ */
+ public void setName(final String name)
+ {
+ this.name = name;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString()
+ {
+ return Employee.class.getSimpleName() + " [id=" + id + ", name=" + name + "]";
+ }
+}
Added: projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmbeddedEmployeePK.java
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmbeddedEmployeePK.java (rev 0)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmbeddedEmployeePK.java 2010-05-19 22:20:38 UTC (rev 105004)
@@ -0,0 +1,155 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.employeeregistry.chyy.mapping;
+
+import java.io.Serializable;
+
+import javax.persistence.Column;
+import javax.persistence.Embeddable;
+import javax.persistence.EmbeddedId;
+
+/**
+ * Composite primary key class to be used as
+ * {@link EmbeddedId} on {@link EmployeeWithExternalCompositePK}.
+ * The instance members here will together compose
+ * an identity in the database (primary key).
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Embeddable
+// Note to JPA that we're intended to be embedded into an Entity
+// class as a PK
+public class EmbeddedEmployeePK implements Serializable
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * serialVersionUID
+ */
+ private static final long serialVersionUID = 1L;
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Last Name
+ */
+ @Column
+ private String lastName;
+
+ /**
+ * Social Security Number (United States Federal ID)
+ */
+ @Column
+ private Long ssn;
+
+ //-------------------------------------------------------------------------------------||
+ // Functional Methods ----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * @return the lastName
+ */
+ public String getLastName()
+ {
+ return lastName;
+ }
+
+ /**
+ * @param lastName the lastName to set
+ */
+ public void setLastName(String lastName)
+ {
+ this.lastName = lastName;
+ }
+
+ /**
+ * @return the ssn
+ */
+ public Long getSsn()
+ {
+ return ssn;
+ }
+
+ /**
+ * @param ssn the ssn to set
+ */
+ public void setSsn(Long ssn)
+ {
+ this.ssn = ssn;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Overridden Implementations ---------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * {@inheritDoc}
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode()
+ {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
+ result = prime * result + ((ssn == null) ? 0 : ssn.hashCode());
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ EmbeddedEmployeePK other = (EmbeddedEmployeePK) obj;
+ if (lastName == null)
+ {
+ if (other.lastName != null)
+ return false;
+ }
+ else if (!lastName.equals(other.lastName))
+ return false;
+ if (ssn == null)
+ {
+ if (other.ssn != null)
+ return false;
+ }
+ else if (!ssn.equals(other.ssn))
+ return false;
+ return true;
+ }
+
+}
Added: projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeType.java
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeType.java (rev 0)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeType.java 2010-05-19 22:20:38 UTC (rev 105004)
@@ -0,0 +1,35 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.employeeregistry.chyy.mapping;
+
+import javax.persistence.Enumerated;
+
+/**
+ * Types of employees in the system. Used to show {@link Enumerated}
+ * in the entity {@link EmployeeWithProperties}.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public enum EmployeeType {
+ MANAGER, PEON;
+}
Added: projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithEmbeddedPK.java
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithEmbeddedPK.java (rev 0)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithEmbeddedPK.java 2010-05-19 22:20:38 UTC (rev 105004)
@@ -0,0 +1,97 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.employeeregistry.chyy.mapping;
+
+import javax.persistence.EmbeddedId;
+import javax.persistence.Entity;
+
+/**
+ * Represents an Employee in the system. The identity
+ * (primary key) is determined by embedded properties
+ * via the {@link EmbeddedEmployeePK}.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Entity
+// Mark that we're an Entity Bean, EJB's integration point
+// with Java Persistence
+public class EmployeeWithEmbeddedPK
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Last Name
+ */
+ @EmbeddedId
+ private EmbeddedEmployeePK id;
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Default constructor, required by JPA
+ */
+ public EmployeeWithEmbeddedPK()
+ {
+
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Accessors / Mutators ---------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * @return the id
+ */
+ public EmbeddedEmployeePK getId()
+ {
+ return id;
+ }
+
+ /**
+ * @param id the id to set
+ */
+ public void setId(final EmbeddedEmployeePK id)
+ {
+ this.id = id;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Overridden Implementations ---------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * {@inheritDoc}
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString()
+ {
+ return EmployeeWithEmbeddedPK.class.getSimpleName() + " [id=" + id + "]";
+ }
+
+}
Added: projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithExternalCompositePK.java
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithExternalCompositePK.java (rev 0)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithExternalCompositePK.java 2010-05-19 22:20:38 UTC (rev 105004)
@@ -0,0 +1,122 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.employeeregistry.chyy.mapping;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.IdClass;
+
+/**
+ * Represents an Employee in the system. The identity
+ * (primary key) is determined by composite properties
+ * defined by {@link ExternalEmployeePK}.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Entity
+// Mark that we're an Entity Bean, EJB's integration point
+// with Java Persistence
+ at IdClass(ExternalEmployeePK.class)
+// Use a composite primary key using a custom PK class
+public class EmployeeWithExternalCompositePK
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Last Name
+ */
+ @Id
+ private String lastName;
+
+ /**
+ * Social Security Number (United States Federal ID)
+ */
+ @Id
+ private Long ssn;
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Default constructor, required by JPA
+ */
+ public EmployeeWithExternalCompositePK()
+ {
+
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Accessors / Mutators ---------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * @return the lastName
+ */
+ public String getLastName()
+ {
+ return lastName;
+ }
+
+ /**
+ * @param lastName the lastName to set
+ */
+ public void setLastName(final String lastName)
+ {
+ this.lastName = lastName;
+ }
+
+ /**
+ * @return the ssn
+ */
+ public Long getSsn()
+ {
+ return ssn;
+ }
+
+ /**
+ * @param ssn the ssn to set
+ */
+ public void setSsn(final Long ssn)
+ {
+ this.ssn = ssn;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Overridden Implementations ---------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * {@inheritDoc}
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString()
+ {
+ return EmployeeWithExternalCompositePK.class.getSimpleName() + " [lastName=" + lastName + ", ssn=" + ssn + "]";
+ }
+
+}
Added: projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithMappedSuperClassId.java
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithMappedSuperClassId.java (rev 0)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithMappedSuperClassId.java 2010-05-19 22:20:38 UTC (rev 105004)
@@ -0,0 +1,107 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.employeeregistry.chyy.mapping;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.Table;
+
+import org.jboss.ejb3.examples.testsupport.entity.AutogenIdentityBase;
+import org.jboss.ejb3.examples.testsupport.entity.IdentityBase;
+
+/**
+ * Represents an Employee in the system. Inherits the
+ * primary key support from {@link IdentityBase#getId()}.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Entity
+// Mark that we're an Entity Bean, EJB's integration point
+// with Java Persistence
+ at Table(name = "employees_with_autogen_pk")
+// Explicitly denote the name of the table in the DB
+public class EmployeeWithMappedSuperClassId extends AutogenIdentityBase
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Name of the employee
+ */
+ // We can use @Column.name to denote the name of the column in the DB
+ @Column(name = "employee_name")
+ private String name;
+
+ //-------------------------------------------------------------------------------------||
+ // Constructor ------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Default constructor, required by JPA
+ */
+ public EmployeeWithMappedSuperClassId()
+ {
+
+ }
+
+ /**
+ * Convenience constructor
+ */
+ public EmployeeWithMappedSuperClassId(final String name)
+ {
+ // Set
+ this.name = name;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Accessors / Mutators ---------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * @return the name
+ */
+ public String getName()
+ {
+ return name;
+ }
+
+ /**
+ * @param name the name to set
+ */
+ public void setName(final String name)
+ {
+ this.name = name;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString()
+ {
+ return EmployeeWithMappedSuperClassId.class.getSimpleName() + " [id=" + this.getId() + ", name=" + name + "]";
+ }
+}
Added: projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithProperties.java
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithProperties.java (rev 0)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/EmployeeWithProperties.java 2010-05-19 22:20:38 UTC (rev 105004)
@@ -0,0 +1,195 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.employeeregistry.chyy.mapping;
+
+import java.util.Arrays;
+import java.util.Date;
+
+import javax.persistence.Basic;
+import javax.persistence.Entity;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Lob;
+import javax.persistence.Temporal;
+import javax.persistence.TemporalType;
+import javax.persistence.Transient;
+
+/**
+ * Represents an Employee with a series of properties to
+ * show JPA Mapping metadata.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Entity
+public class EmployeeWithProperties
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Primary key
+ */
+ @Id
+ @GeneratedValue
+ // Automatically manage PK creation for us
+ private Long id;
+
+ /**
+ * Description of what the Employee's currently
+ * working on. We don't need to store this in the DB.
+ */
+ @Transient
+ // Don't persist this
+ private String currentAssignment;
+
+ /**
+ * Picture of the employee used in ID cards.
+ */
+ @Lob
+ // Note that this is a binary large object
+ @Basic(fetch = FetchType.LAZY, optional = true)
+ // Don't load this by default; it's an expensive operation.
+ // Only load when requested.
+ private byte[] image;
+
+ /**
+ * Type of employee
+ */
+ @Enumerated(EnumType.STRING)
+ // Show that this is an enumerated value, and the value to
+ // be put in the DB is the value of the enumeration toString().
+ private EmployeeType type;
+
+ /**
+ * Date the employee joined the company
+ */
+ @Temporal(TemporalType.DATE)
+ // Note that we should map this as an SQL Date field;
+ // could also be SQL Time or Timestamp
+ private Date since;
+
+ //-------------------------------------------------------------------------------------||
+ // Accessors / Mutators ---------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * @return the id
+ */
+ public Long getId()
+ {
+ return id;
+ }
+
+ /**
+ * @param id the id to set
+ */
+ public void setId(final Long id)
+ {
+ this.id = id;
+ }
+
+ /**
+ * @return the currentAssignment
+ */
+ public String getCurrentAssignment()
+ {
+ return currentAssignment;
+ }
+
+ /**
+ * @param currentAssignment the currentAssignment to set
+ */
+ public void setCurrentAssignment(final String currentAssignment)
+ {
+ this.currentAssignment = currentAssignment;
+ }
+
+ /**
+ * @return the image
+ */
+ public byte[] getImage()
+ {
+ return image;
+ }
+
+ /**
+ * @param image the image to set
+ */
+ public void setImage(final byte[] image)
+ {
+ this.image = image;
+ }
+
+ /**
+ * @return the type
+ */
+ public EmployeeType getType()
+ {
+ return type;
+ }
+
+ /**
+ * @param type the type to set
+ */
+ public void setType(final EmployeeType type)
+ {
+ this.type = type;
+ }
+
+ /**
+ * @return the since
+ */
+ public Date getSince()
+ {
+ return since;
+ }
+
+ /**
+ * @param since the since to set
+ */
+ public void setSince(final Date since)
+ {
+ this.since = since;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Overridden Implementations ---------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * {@inheritDoc}
+ * @see java.lang.Object#toString()
+ */
+ @Override
+ public String toString()
+ {
+ return "EmployeeWithProperties [currentAssignment=" + currentAssignment + ", id=" + id + ", image="
+ + Arrays.toString(image) + ", since=" + since + ", type=" + type + "]";
+ }
+
+}
Added: projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/ExternalEmployeePK.java
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/ExternalEmployeePK.java (rev 0)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/chyy/mapping/ExternalEmployeePK.java 2010-05-19 22:20:38 UTC (rev 105004)
@@ -0,0 +1,148 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.employeeregistry.chyy.mapping;
+
+import java.io.Serializable;
+
+import javax.persistence.IdClass;
+
+/**
+ * Composite primary key class to be used as
+ * {@link IdClass} on {@link EmployeeWithExternalCompositePK}.
+ * The instance members here will together compose
+ * an identity in the database (primary key).
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class ExternalEmployeePK implements Serializable
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * serialVersionUID
+ */
+ private static final long serialVersionUID = 1L;
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Last Name
+ */
+ private String lastName;
+
+ /**
+ * Social Security Number (United States Federal ID)
+ */
+ private Long ssn;
+
+ //-------------------------------------------------------------------------------------||
+ // Functional Methods ----------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * @return the lastName
+ */
+ public String getLastName()
+ {
+ return lastName;
+ }
+
+ /**
+ * @param lastName the lastName to set
+ */
+ public void setLastName(String lastName)
+ {
+ this.lastName = lastName;
+ }
+
+ /**
+ * @return the ssn
+ */
+ public Long getSsn()
+ {
+ return ssn;
+ }
+
+ /**
+ * @param ssn the ssn to set
+ */
+ public void setSsn(Long ssn)
+ {
+ this.ssn = ssn;
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Overridden Implementations ---------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * {@inheritDoc}
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode()
+ {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
+ result = prime * result + ((ssn == null) ? 0 : ssn.hashCode());
+ return result;
+ }
+
+ /**
+ * {@inheritDoc}
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ ExternalEmployeePK other = (ExternalEmployeePK) obj;
+ if (lastName == null)
+ {
+ if (other.lastName != null)
+ return false;
+ }
+ else if (!lastName.equals(other.lastName))
+ return false;
+ if (ssn == null)
+ {
+ if (other.ssn != null)
+ return false;
+ }
+ else if (!ssn.equals(other.ssn))
+ return false;
+ return true;
+ }
+
+}
Deleted: projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/entity/Employee.java
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/entity/Employee.java 2010-05-19 21:54:20 UTC (rev 105003)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/main/java/org/jboss/ejb3/examples/employeeregistry/entity/Employee.java 2010-05-19 22:20:38 UTC (rev 105004)
@@ -1,123 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2010, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.ejb3.examples.employeeregistry.entity;
-
-import javax.persistence.Entity;
-import javax.persistence.Id;
-
-/**
- * Represents an Employee in the system. Modeled as a simple
- * value object with some additional EJB and JPA annotations.
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
- at Entity
-// Mark that we're an Entity Bean, EJB's integration point
-// with Java Persistence
-public class Employee
-{
-
- //-------------------------------------------------------------------------------------||
- // Instance Members -------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Primary key of this entity
- */
- @Id
- // Mark that this field is the primary key
- private Long id;
-
- /**
- * Name of the employee
- */
- private String name;
-
- //-------------------------------------------------------------------------------------||
- // Constructor ------------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Default constructor, required by JPA
- */
- public Employee()
- {
-
- }
-
- /**
- * Convenience constructor
- */
- public Employee(final long id, final String name)
- {
- // Set
- this.id = id;
- this.name = name;
- }
-
- //-------------------------------------------------------------------------------------||
- // Accessors / Mutators ---------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * @return the id
- */
- public Long getId()
- {
- return id;
- }
-
- /**
- * @param id the id to set
- */
- public void setId(final Long id)
- {
- this.id = id;
- }
-
- /**
- * @return the name
- */
- public String getName()
- {
- return name;
- }
-
- /**
- * @param name the name to set
- */
- public void setName(final String name)
- {
- this.name = name;
- }
-
- /**
- * {@inheritDoc}
- * @see java.lang.Object#toString()
- */
- @Override
- public String toString()
- {
- return "Employee [id=" + id + ", name=" + name + "]";
- }
-}
Deleted: projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java 2010-05-19 21:54:20 UTC (rev 105003)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java 2010-05-19 22:20:38 UTC (rev 105004)
@@ -1,375 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2010, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.ejb3.examples.employeeregistry;
-
-import java.util.concurrent.Callable;
-import java.util.logging.Logger;
-
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.persistence.EntityManager;
-
-import org.jboss.arquillian.api.Deployment;
-import org.jboss.arquillian.api.RunMode;
-import org.jboss.arquillian.api.RunModeType;
-import org.jboss.arquillian.junit.Arquillian;
-import org.jboss.ejb3.examples.employeeregistry.entity.Employee;
-import org.jboss.ejb3.examples.testsupport.dbquery.EntityManagerExposingBean;
-import org.jboss.ejb3.examples.testsupport.dbquery.EntityManagerExposingLocalBusiness;
-import org.jboss.ejb3.examples.testsupport.txwrap.TaskExecutionException;
-import org.jboss.ejb3.examples.testsupport.txwrap.TxWrappingBean;
-import org.jboss.ejb3.examples.testsupport.txwrap.TxWrappingLocalBusiness;
-import org.jboss.shrinkwrap.api.ShrinkWrap;
-import org.jboss.shrinkwrap.api.spec.JavaArchive;
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-/**
- * Tests to ensure that we can do simple CRUD operations
- * upon an object view (Entity beans), and see our changes persisted
- * across transactions.
- *
- * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
- * @version $Revision: $
- */
- at RunWith(Arquillian.class)
- at RunMode(RunModeType.LOCAL)
-public class EmployeeIntegrationTest
-{
- //-------------------------------------------------------------------------------------||
- // Class Members ----------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Logger
- */
- private static final Logger log = Logger.getLogger(EmployeeIntegrationTest.class.getName());
-
- /**
- * Naming Context
- * @deprecated Remove when Arquillian will inject the EJB proxies
- */
- @Deprecated
- private static Context jndiContext;
-
- /**
- * The Deployment into the EJB Container
- */
- @Deployment
- public static JavaArchive getDeployment()
- {
- final JavaArchive archive = ShrinkWrap.create("test.jar", JavaArchive.class).addPackages(true,
- Employee.class.getPackage()).addManifestResource("persistence.xml").addPackages(false,
- TxWrappingLocalBusiness.class.getPackage(), EntityManagerExposingBean.class.getPackage());
- log.info(archive.toString(true));
- return archive;
- }
-
- /*
- * Data for our tests
- */
-
- private static final long ID_DAVE = 1L;
-
- private static final long ID_JOSH = 2L;
-
- private static final long ID_RICK = 3L;
-
- private static final String NAME_DAVE = "Dave";
-
- private static final String NAME_DAVE_NEW = "Dave - The Good Doctor";
-
- private static final String NAME_JOSH = "Josh";
-
- private static final String NAME_RICK = "Rick, Jr.";
-
- //-------------------------------------------------------------------------------------||
- // Instance Members -------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * EJB which wraps supplied {@link Callable} instances inside of a new Tx
- */
- // TODO: Support Injection of @EJB here when Arquillian for Embedded JBossAS will support it
- private TxWrappingLocalBusiness txWrapper;
-
- /**
- * EJB which provides direct access to an {@link EntityManager}'s method for use in testing.
- * Must be called inside an existing Tx so that returned entities are not detached.
- */
- // TODO: Support Injection of @EJB here when Arquillian for Embedded JBossAS will support it
- private EntityManagerExposingLocalBusiness emHook;
-
- //-------------------------------------------------------------------------------------||
- // Lifecycle --------------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Performs suite-wide initialization
- */
- @BeforeClass
- public static void init() throws Exception
- {
- // After the server is up, we don't need to pass any explicit properties
- jndiContext = new InitialContext();
- }
-
- /**
- * Manually looks up EJBs in JNDI and assigns them
- */
- @Before
- public void injectEjbsAndClearDB() throws Throwable
- {
- // Fake injection by doing manual lookups for the time being
- //TODO Deprecated portion
- txWrapper = (TxWrappingLocalBusiness) jndiContext.lookup(TxWrappingBean.class.getSimpleName() + "/local");
- emHook = (EntityManagerExposingLocalBusiness) jndiContext.lookup(EntityManagerExposingBean.class.getSimpleName()
- + "/local");
-
- // Clear all employees before running, just in case
- this.clearAllEmployees();
- }
-
- /**
- * Issues a deletion to remove all employees from persistent storage
- * @throws Throwable
- */
- @After
- public void clearAllEmployees() throws Throwable
- {
- // Clear the DB of all Employees
- try
- {
- txWrapper.wrapInTx(new Callable<Void>()
- {
-
- @Override
- public Void call() throws Exception
- {
- // JPA QL String to remove all Employees
- final EntityManager em = emHook.getEntityManager();
- em.createQuery("DELETE FROM " + Employee.class.getSimpleName() + " o").executeUpdate();
- return null;
- }
-
- });
- }
- catch (final TaskExecutionException tee)
- {
- // Unwrap
- throw tee.getCause();
- }
- }
-
- //-------------------------------------------------------------------------------------||
- // Tests ------------------------------------------------------------------------------||
- //-------------------------------------------------------------------------------------||
-
- /**
- * Tests that we can use the {@link EntityManager} to perform simple
- * CRUD (Create, remove, update, delete) operations on an object view,
- * and these changes will be persisted as expected.
- */
- @Test
- public void persistAndModifyEmployees() throws Throwable
- {
-
- try
- {
-
- // Execute the addition of the employees, and conditional checks, in the context of a Transaction
- txWrapper.wrapInTx(new Callable<Void>()
- {
-
- @Override
- public Void call() throws Exception
- {
- // Create a few plain instances
- final Employee josh = new Employee(ID_DAVE, NAME_DAVE);
- final Employee dave = new Employee(ID_JOSH, NAME_JOSH);
- final Employee rick = new Employee(ID_RICK, NAME_RICK);
-
- // Get the EntityManager from our test hook
- final EntityManager em = emHook.getEntityManager();
-
- // Now first check if any employees are found in the underlying persistent
- // storage (shouldn't be)
- Assert
- .assertNull("Employees should not have been added to the EM yet", em.find(Employee.class, ID_DAVE));
-
- // Check if the object is managed (shouldn't be)
- Assert.assertFalse("Employee should not be managed yet", em.contains(josh));
-
- // Now persist the employees
- em.persist(dave);
- em.persist(josh);
- em.persist(rick);
- log.info("Added: " + rick + dave + josh);
-
- // The employees should be managed now
- Assert.assertTrue("Employee should be managed now, after call to persist", em.contains(josh));
-
- // Return
- return null;
- }
- });
-
- // Now change Employee Dave's name in a Tx; we'll verify the changes were flushed to the DB later
- txWrapper.wrapInTx(new Callable<Void>()
- {
-
- @Override
- public Void call() throws Exception
- {
- // Get an EM
- final EntityManager em = emHook.getEntityManager();
-
- // Look up "Dave" by ID from the EM
- final Employee dave = em.find(Employee.class, ID_DAVE);
-
- // Change Dave's name
- dave.setName(NAME_DAVE_NEW);
- log.info("Changing Dave's name: " + dave);
-
- // That's it - the new name should be flushed to the DB when the Tx completes
- return null;
- }
- });
-
- // Since we've changed Dave's name in the last transaction, ensure that we see the changes
- // have been flushed and we can see them from a new Tx.
- txWrapper.wrapInTx(new Callable<Void>()
- {
-
- @Override
- public Void call() throws Exception
- {
- // Get an EM
- final EntityManager em = emHook.getEntityManager();
-
- // Make a new "Dave" as a detached object with same primary key, but a different name
- final Employee dave = new Employee(ID_DAVE, NAME_DAVE_NEW);
-
- // Merge these changes on the detached instance with the DB
- em.merge(dave);
-
- // Ensure we see the name change
- Assert.assertEquals("Employee Dave's name should have been changed", NAME_DAVE_NEW, dave.getName());
-
- // Now we'll detach Dave from the EM, this makes the object no longer managed
- em.detach(dave);
-
- // Change Dave's name again to some dummy value. Because the object is
- // detached and no longer managed, we should not see this new value
- // synchronized with the DB
- dave.setName("A name we shouldn't see flushed to persistence");
- log.info("Changing Dave's name after detached: " + dave);
-
- // Return
- return null;
- }
- });
-
- // Another check. We changed Dave's name when the entity was no longer
- // managed and attached to an EM, so ensure that any changes we made
- // were not flushed out
- txWrapper.wrapInTx(new Callable<Void>()
- {
-
- @Override
- public Void call() throws Exception
- {
- // Get an EM
- final EntityManager em = emHook.getEntityManager();
-
- // Make a new "Dave" instance
- final Employee dave = em.find(Employee.class, ID_DAVE);
- log.info("Lookup of Dave after we changed his name on a detached instance: " + dave);
-
- // Ensure that the last name change we gave to Dave did not take affect
- Assert
- .assertEquals("Detached object values should not have been flushed", NAME_DAVE_NEW, dave.getName());
-
- // Return
- return null;
-
- }
- });
-
- // Uh oh, Rick has decided to leave the company. Let's delete his record.
- txWrapper.wrapInTx(new Callable<Void>()
- {
-
- @Override
- public Void call() throws Exception
- {
- // Get an EM
- final EntityManager em = emHook.getEntityManager();
-
- // Look up Rick
- final Employee rick = em.find(Employee.class, ID_RICK);
-
- // Remove
- em.remove(rick);
- log.info("Deleted: " + rick);
-
- // Return
- return null;
-
- }
- });
-
- // Ensure we can no longer find Rick in the DB
- txWrapper.wrapInTx(new Callable<Void>()
- {
-
- @Override
- public Void call() throws Exception
- {
- // Get an EM
- final EntityManager em = emHook.getEntityManager();
-
- // Look up Rick
- final Employee rick = em.find(Employee.class, ID_RICK);
-
- // Assert
- Assert.assertNull("Rick should have been removed from the DB", rick);
-
- // Return
- return null;
-
- }
- });
-
- }
- catch (final TaskExecutionException tee)
- {
- // Unwrap
- throw tee.getCause();
- }
-
- }
-}
Added: projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java
===================================================================
--- projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java (rev 0)
+++ projects/ejb-book/trunk/chxx-employeeregistry/src/test/java/org/jboss/ejb3/examples/employeeregistry/EmployeeIntegrationTest.java 2010-05-19 22:20:38 UTC (rev 105004)
@@ -0,0 +1,653 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.employeeregistry;
+
+import java.util.Calendar;
+import java.util.Date;
+import java.util.concurrent.Callable;
+import java.util.logging.Logger;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.persistence.EmbeddedId;
+import javax.persistence.EntityManager;
+import javax.persistence.GeneratedValue;
+import javax.persistence.IdClass;
+
+import org.jboss.arquillian.api.Deployment;
+import org.jboss.arquillian.api.RunMode;
+import org.jboss.arquillian.api.RunModeType;
+import org.jboss.arquillian.junit.Arquillian;
+import org.jboss.ejb3.examples.employeeregistry.chxx.entitymanager.Employee;
+import org.jboss.ejb3.examples.employeeregistry.chyy.mapping.EmbeddedEmployeePK;
+import org.jboss.ejb3.examples.employeeregistry.chyy.mapping.EmployeeType;
+import org.jboss.ejb3.examples.employeeregistry.chyy.mapping.EmployeeWithEmbeddedPK;
+import org.jboss.ejb3.examples.employeeregistry.chyy.mapping.EmployeeWithExternalCompositePK;
+import org.jboss.ejb3.examples.employeeregistry.chyy.mapping.EmployeeWithMappedSuperClassId;
+import org.jboss.ejb3.examples.employeeregistry.chyy.mapping.EmployeeWithProperties;
+import org.jboss.ejb3.examples.employeeregistry.chyy.mapping.ExternalEmployeePK;
+import org.jboss.ejb3.examples.testsupport.dbquery.EntityManagerExposingBean;
+import org.jboss.ejb3.examples.testsupport.dbquery.EntityManagerExposingLocalBusiness;
+import org.jboss.ejb3.examples.testsupport.entity.IdentityBase;
+import org.jboss.ejb3.examples.testsupport.txwrap.TaskExecutionException;
+import org.jboss.ejb3.examples.testsupport.txwrap.TxWrappingBean;
+import org.jboss.ejb3.examples.testsupport.txwrap.TxWrappingLocalBusiness;
+import org.jboss.shrinkwrap.api.ShrinkWrap;
+import org.jboss.shrinkwrap.api.spec.JavaArchive;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+/**
+ * Tests to ensure that we can do simple CRUD operations
+ * upon an object view (Entity beans), and see our changes persisted
+ * across transactions.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at RunWith(Arquillian.class)
+ at RunMode(RunModeType.LOCAL)
+public class EmployeeIntegrationTest
+{
+ //-------------------------------------------------------------------------------------||
+ // Class Members ----------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Logger
+ */
+ private static final Logger log = Logger.getLogger(EmployeeIntegrationTest.class.getName());
+
+ /**
+ * Naming Context
+ * @deprecated Remove when Arquillian will inject the EJB proxies
+ */
+ @Deprecated
+ private static Context jndiContext;
+
+ /**
+ * The Deployment into the EJB Container
+ */
+ @Deployment
+ public static JavaArchive getDeployment()
+ {
+ final JavaArchive archive = ShrinkWrap.create("test.jar", JavaArchive.class).addPackages(true,
+ Employee.class.getPackage(), EmployeeWithMappedSuperClassId.class.getPackage()).addManifestResource(
+ "persistence.xml").addPackages(false, TxWrappingLocalBusiness.class.getPackage(),
+ EntityManagerExposingBean.class.getPackage());
+ log.info(archive.toString(true));
+ return archive;
+ }
+
+ /*
+ * Data for our tests
+ */
+
+ private static final long ID_DAVE = 1L;
+
+ private static final long ID_JOSH = 2L;
+
+ private static final long ID_RICK = 3L;
+
+ private static final String NAME_DAVE = "Dave";
+
+ private static final String NAME_DAVE_NEW = "Dave - The Good Doctor";
+
+ private static final String NAME_JOSH = "Josh";
+
+ private static final String NAME_RICK = "Rick, Jr.";
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * EJB which wraps supplied {@link Callable} instances inside of a new Tx
+ */
+ // TODO: Support Injection of @EJB here when Arquillian for Embedded JBossAS will support it
+ private TxWrappingLocalBusiness txWrapper;
+
+ /**
+ * EJB which provides direct access to an {@link EntityManager}'s method for use in testing.
+ * Must be called inside an existing Tx so that returned entities are not detached.
+ */
+ // TODO: Support Injection of @EJB here when Arquillian for Embedded JBossAS will support it
+ private EntityManagerExposingLocalBusiness emHook;
+
+ //-------------------------------------------------------------------------------------||
+ // Lifecycle --------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Performs suite-wide initialization
+ */
+ @BeforeClass
+ public static void init() throws Exception
+ {
+ // After the server is up, we don't need to pass any explicit properties
+ jndiContext = new InitialContext();
+ }
+
+ /**
+ * Manually looks up EJBs in JNDI and assigns them
+ */
+ @Before
+ public void injectEjbsAndClearDB() throws Throwable
+ {
+ // Fake injection by doing manual lookups for the time being
+ //TODO Deprecated portion
+ txWrapper = (TxWrappingLocalBusiness) jndiContext.lookup(TxWrappingBean.class.getSimpleName() + "/local");
+ emHook = (EntityManagerExposingLocalBusiness) jndiContext.lookup(EntityManagerExposingBean.class.getSimpleName()
+ + "/local");
+
+ // Clear all employees before running, just in case
+ this.clearAllEmployees();
+ }
+
+ /**
+ * Issues a deletion to remove all employees from persistent storage
+ * @throws Throwable
+ */
+ @After
+ public void clearAllEmployees() throws Throwable
+ {
+ // Clear the DB of all Employees
+ try
+ {
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // JPA QL String to remove all Employees
+ final EntityManager em = emHook.getEntityManager();
+ em.createQuery("DELETE FROM " + Employee.class.getSimpleName() + " o").executeUpdate();
+ em.createQuery("DELETE FROM " + EmployeeWithMappedSuperClassId.class.getSimpleName() + " o")
+ .executeUpdate();
+ em.createQuery("DELETE FROM " + EmployeeWithExternalCompositePK.class.getSimpleName() + " o");
+ em.createQuery("DELETE FROM " + EmployeeWithProperties.class.getSimpleName() + " o")
+
+ .executeUpdate();
+ return null;
+ }
+
+ });
+ }
+ catch (final TaskExecutionException tee)
+ {
+ // Unwrap
+ throw tee.getCause();
+ }
+ }
+
+ //-------------------------------------------------------------------------------------||
+ // Tests ------------------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Tests that we can use the {@link EntityManager} to perform simple
+ * CRUD (Create, remove, update, delete) operations on an object view,
+ * and these changes will be persisted as expected.
+ */
+ @Test
+ public void persistAndModifyEmployees() throws Throwable
+ {
+
+ try
+ {
+
+ // Execute the addition of the employees, and conditional checks, in the context of a Transaction
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Create a few plain instances
+ final Employee josh = new Employee(ID_DAVE, NAME_DAVE);
+ final Employee dave = new Employee(ID_JOSH, NAME_JOSH);
+ final Employee rick = new Employee(ID_RICK, NAME_RICK);
+
+ // Get the EntityManager from our test hook
+ final EntityManager em = emHook.getEntityManager();
+
+ // Now first check if any employees are found in the underlying persistent
+ // storage (shouldn't be)
+ Assert
+ .assertNull("Employees should not have been added to the EM yet", em.find(Employee.class, ID_DAVE));
+
+ // Check if the object is managed (shouldn't be)
+ Assert.assertFalse("Employee should not be managed yet", em.contains(josh));
+
+ // Now persist the employees
+ em.persist(dave);
+ em.persist(josh);
+ em.persist(rick);
+ log.info("Added: " + rick + dave + josh);
+
+ // The employees should be managed now
+ Assert.assertTrue("Employee should be managed now, after call to persist", em.contains(josh));
+
+ // Return
+ return null;
+ }
+ });
+
+ // Now change Employee Dave's name in a Tx; we'll verify the changes were flushed to the DB later
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Get an EM
+ final EntityManager em = emHook.getEntityManager();
+
+ // Look up "Dave" by ID from the EM
+ final Employee dave = em.find(Employee.class, ID_DAVE);
+
+ // Change Dave's name
+ dave.setName(NAME_DAVE_NEW);
+ log.info("Changing Dave's name: " + dave);
+
+ // That's it - the new name should be flushed to the DB when the Tx completes
+ return null;
+ }
+ });
+
+ // Since we've changed Dave's name in the last transaction, ensure that we see the changes
+ // have been flushed and we can see them from a new Tx.
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Get an EM
+ final EntityManager em = emHook.getEntityManager();
+
+ // Make a new "Dave" as a detached object with same primary key, but a different name
+ final Employee dave = new Employee(ID_DAVE, NAME_DAVE_NEW);
+
+ // Merge these changes on the detached instance with the DB
+ em.merge(dave);
+
+ // Ensure we see the name change
+ Assert.assertEquals("Employee Dave's name should have been changed", NAME_DAVE_NEW, dave.getName());
+
+ // Now we'll detach Dave from the EM, this makes the object no longer managed
+ em.detach(dave);
+
+ // Change Dave's name again to some dummy value. Because the object is
+ // detached and no longer managed, we should not see this new value
+ // synchronized with the DB
+ dave.setName("A name we shouldn't see flushed to persistence");
+ log.info("Changing Dave's name after detached: " + dave);
+
+ // Return
+ return null;
+ }
+ });
+
+ // Another check. We changed Dave's name when the entity was no longer
+ // managed and attached to an EM, so ensure that any changes we made
+ // were not flushed out
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Get an EM
+ final EntityManager em = emHook.getEntityManager();
+
+ // Make a new "Dave" instance
+ final Employee dave = em.find(Employee.class, ID_DAVE);
+ log.info("Lookup of Dave after we changed his name on a detached instance: " + dave);
+
+ // Ensure that the last name change we gave to Dave did not take affect
+ Assert
+ .assertEquals("Detached object values should not have been flushed", NAME_DAVE_NEW, dave.getName());
+
+ // Return
+ return null;
+
+ }
+ });
+
+ // Uh oh, Rick has decided to leave the company. Let's delete his record.
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Get an EM
+ final EntityManager em = emHook.getEntityManager();
+
+ // Look up Rick
+ final Employee rick = em.find(Employee.class, ID_RICK);
+
+ // Remove
+ em.remove(rick);
+ log.info("Deleted: " + rick);
+
+ // Return
+ return null;
+
+ }
+ });
+
+ // Ensure we can no longer find Rick in the DB
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Get an EM
+ final EntityManager em = emHook.getEntityManager();
+
+ // Look up Rick
+ final Employee rick = em.find(Employee.class, ID_RICK);
+
+ // Assert
+ Assert.assertNull("Rick should have been removed from the DB", rick);
+
+ // Return
+ return null;
+
+ }
+ });
+
+ }
+ catch (final TaskExecutionException tee)
+ {
+ // Unwrap
+ throw tee.getCause();
+ }
+
+ }
+
+ /**
+ * Shows usage of JPA autogeneration of primary keys, using
+ * {@link EmployeeWithMappedSuperClassId} which inherits PK support from
+ * {@link IdentityBase#getId()}.
+ * @throws Throwable
+ */
+ @Test
+ public void autogenPrimaryKeyFromMappedSuperClass() throws Throwable
+ {
+ try
+ {
+ // Create a new Employee, and let JPA give us the PK value
+ final Long id = txWrapper.wrapInTx(new Callable<Long>()
+ {
+
+ @Override
+ public Long call() throws Exception
+ {
+ // Make a new Employee
+ final EmployeeWithMappedSuperClassId alrubinger = new EmployeeWithMappedSuperClassId(
+ "Andrew Lee Rubinger");
+
+ // Ensure we have no ID now
+ Assert.assertNull("Primary key should not be set yet", alrubinger.getId());
+
+ // Persist
+ emHook.getEntityManager().persist(alrubinger);
+
+ // Now show that JPA gave us a primary key as generated
+ final Long id = alrubinger.getId();
+ Assert.assertNotNull("Persisting an entity with PK " + GeneratedValue.class.getName()
+ + " should be created", id);
+ log.info("Persisted: " + alrubinger);
+
+ // Return
+ return id;
+ }
+
+ });
+
+ // Ensure we can look up this new entity by the PK we've been given
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Look up the Employee by the ID we just gave
+ final EmployeeWithMappedSuperClassId employee = emHook.getEntityManager().find(
+ EmployeeWithMappedSuperClassId.class, id);
+
+ // Ensure found
+ Assert.assertNotNull("Employee should be able to be looked up by PK", employee);
+
+ // Return
+ return null;
+ }
+
+ });
+ }
+ catch (final TaskExecutionException tee)
+ {
+ // Unwrap
+ throw tee.getCause();
+ }
+ }
+
+ /**
+ * Shows usage of an entity which gets its identity via an
+ * {@link IdClass} - {@link ExternalEmployeePK}.
+ * @throws Throwable
+ */
+ @Test
+ public void externalCompositePrimaryKey() throws Throwable
+ {
+ try
+ {
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Define the values to compose a primary key identity
+ final String lastName = "Rubinger";
+ final Long ssn = 100L; // Not real ;)
+
+ // Create a new Employee which uses a custom @IdClass
+ final EmployeeWithExternalCompositePK employee = new EmployeeWithExternalCompositePK();
+ employee.setLastName(lastName);
+ employee.setSsn(ssn);
+
+ // Persist
+ final EntityManager em = emHook.getEntityManager();
+ em.persist(employee);
+ log.info("Persisted: " + employee);
+
+ // Now look up using our custom composite PK value class
+ final ExternalEmployeePK pk = new ExternalEmployeePK();
+ pk.setLastName(lastName);
+ pk.setSsn(ssn);
+ final EmployeeWithExternalCompositePK roundtrip = em.find(EmployeeWithExternalCompositePK.class, pk);
+
+ // Ensure found
+ Assert.assertNotNull("Should have been able to look up record via a custom PK composite class",
+ roundtrip);
+
+ // Return
+ return null;
+ }
+
+ });
+ }
+ catch (final TaskExecutionException tee)
+ {
+ // Unwrap
+ throw tee.getCause();
+ }
+ }
+
+ /**
+ * Shows usage of an entity which gets its identity via an
+ * {@link EmbeddedId} - {@link EmployeeWithEmbeddedPK}
+ * @throws Throwable
+ */
+ @Test
+ public void embeddedCompositePrimaryKey() throws Throwable
+ {
+ try
+ {
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Define the values to compose a primary key identity
+ final String lastName = "Rubinger";
+ final Long ssn = 100L; // Not real ;)
+
+ // Create a new Employee which uses an Embedded PK Class
+ final EmployeeWithEmbeddedPK employee = new EmployeeWithEmbeddedPK();
+ final EmbeddedEmployeePK pk = new EmbeddedEmployeePK();
+ pk.setLastName(lastName);
+ pk.setSsn(ssn);
+ employee.setId(pk);
+
+ // Persist
+ final EntityManager em = emHook.getEntityManager();
+ em.persist(employee);
+ log.info("Persisted: " + employee);
+
+ // Now look up using our custom composite PK value class
+ final EmployeeWithEmbeddedPK roundtrip = em.find(EmployeeWithEmbeddedPK.class, pk);
+
+ // Ensure found
+ Assert
+ .assertNotNull("Should have been able to look up record via a custom embedded PK class", roundtrip);
+
+ // Return
+ return null;
+ }
+
+ });
+ }
+ catch (final TaskExecutionException tee)
+ {
+ // Unwrap
+ throw tee.getCause();
+ }
+ }
+
+ /**
+ * Shows usage of an entity with a series of nonstandard
+ * mappings which require additional JPA metadata to show
+ * the ORM layer how things should be represented in the DB.
+ */
+ @Test
+ public void propertyMappings() throws Throwable
+ {
+ // Define the values for our employee
+ final byte[] image = new byte[]
+ {0x00};
+ final Date since = new Date(0L); // Employed since the epoch
+ final EmployeeType type = EmployeeType.PEON;
+ final String currentAssignment = "Learn JPA and EJB!";
+
+ try
+ {
+ final Long id = txWrapper.wrapInTx(new Callable<Long>()
+ {
+
+ @Override
+ public Long call() throws Exception
+ {
+
+ // Create a new Employee
+ final EmployeeWithProperties employee = new EmployeeWithProperties();
+ employee.setImage(image);
+ employee.setSince(since);
+ employee.setType(type);
+ employee.setCurrentAssignment(currentAssignment);
+
+ // Persist
+ final EntityManager em = emHook.getEntityManager();
+ em.persist(employee);
+ log.info("Persisted: " + employee);
+
+ // Get the ID, now that one's been assigned
+ final Long id = employee.getId();
+
+ // Return
+ return id;
+ }
+
+ });
+
+ // Now execute in another Tx, to ensure we get a real DB load from the EM,
+ // and not just a direct reference back to the object we persisted.
+ txWrapper.wrapInTx(new Callable<Void>()
+ {
+
+ @Override
+ public Void call() throws Exception
+ {
+ // Roundtrip lookup
+ final EmployeeWithProperties roundtrip = emHook.getEntityManager()
+ .find(EmployeeWithProperties.class, id);
+ log.info("Roundtrip: " + roundtrip);
+
+ final Calendar suppliedSince = Calendar.getInstance();
+ suppliedSince.setTime(since);
+ final Calendar obtainedSince = Calendar.getInstance();
+ obtainedSince.setTime(roundtrip.getSince());
+
+ // Assert all values are as expected
+ Assert.assertEquals("Binary object was not mapped properly", image[0], roundtrip.getImage()[0]);
+ Assert.assertEquals("Temporal value was not mapped properly", suppliedSince.get(Calendar.YEAR),
+ obtainedSince.get(Calendar.YEAR));
+ Assert.assertEquals("Temporal value was not mapped properly", suppliedSince.get(Calendar.MONTH),
+ obtainedSince.get(Calendar.MONTH));
+ Assert.assertEquals("Temporal value was not mapped properly", suppliedSince.get(Calendar.DATE),
+ obtainedSince.get(Calendar.DATE));
+ Assert.assertEquals("Enumerated value was not as expected", type, roundtrip.getType());
+ Assert.assertNull("Transient property should not have been persisted", roundtrip.getCurrentAssignment());
+
+ // Return
+ return null;
+ }
+ });
+ }
+ catch (final TaskExecutionException tee)
+ {
+ // Unwrap
+ throw tee.getCause();
+ }
+ }
+}
Added: projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/entity/AutogenIdentityBase.java
===================================================================
--- projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/entity/AutogenIdentityBase.java (rev 0)
+++ projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/entity/AutogenIdentityBase.java 2010-05-19 22:20:38 UTC (rev 105004)
@@ -0,0 +1,71 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.examples.testsupport.entity;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.MappedSuperclass;
+
+/**
+ * Base class for all entities with an autogenerated ID
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+ at Entity
+ at MappedSuperclass
+public abstract class AutogenIdentityBase
+{
+
+ //-------------------------------------------------------------------------------------||
+ // Instance Members -------------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * Primary key
+ */
+ @Id
+ @GeneratedValue
+ private Long id;
+
+ //-------------------------------------------------------------------------------------||
+ // Accessors / Mutators ---------------------------------------------------------------||
+ //-------------------------------------------------------------------------------------||
+
+ /**
+ * @return the id
+ */
+ public Long getId()
+ {
+ return id;
+ }
+
+ /**
+ * @param id the id to set
+ */
+ public void setId(final Long id)
+ {
+ this.id = id;
+ }
+
+}
Modified: projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/entity/IdentityBase.java
===================================================================
--- projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/entity/IdentityBase.java 2010-05-19 21:54:20 UTC (rev 105003)
+++ projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/entity/IdentityBase.java 2010-05-19 22:20:38 UTC (rev 105004)
@@ -26,7 +26,7 @@
import javax.persistence.MappedSuperclass;
/**
- * Base class for all entities with an ID
+ * Base class for all entities with a manually-assigned ID
*
* @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
* @version $Revision: $
Modified: projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/txwrap/TxWrappingBean.java
===================================================================
--- projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/txwrap/TxWrappingBean.java 2010-05-19 21:54:20 UTC (rev 105003)
+++ projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/txwrap/TxWrappingBean.java 2010-05-19 22:20:38 UTC (rev 105004)
@@ -61,17 +61,28 @@
// Just delegate along to the tasks in order; now it's executed inside of a Tx
for (final Callable<?> task : tasks)
{
- try
- {
- task.call();
- }
- // Every problem we encounter here becomes an ApplicationException
- // to be unwrapped later by the caller (so the container doesn't wrap
- // in EJBException
- catch (final Throwable e)
- {
- throw new TaskExecutionException(e);
- }
+ this.wrapInTx(task);
}
}
+
+ /**
+ * {@inheritDoc}
+ * @see org.jboss.ejb3.examples.testsupport.txwrap.TxWrappingLocalBusiness#wrapInTx(java.util.concurrent.Callable)
+ */
+ @Override
+ public <T> T wrapInTx(final Callable<T> task) throws IllegalArgumentException, TaskExecutionException
+ {
+ try
+ {
+ // Invoke
+ return task.call();
+ }
+ // Every problem we encounter here becomes an ApplicationException
+ // to be unwrapped later by the caller (so the container doesn't wrap
+ // in EJBException
+ catch (final Throwable e)
+ {
+ throw new TaskExecutionException(e);
+ }
+ }
}
Modified: projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/txwrap/TxWrappingLocalBusiness.java
===================================================================
--- projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/txwrap/TxWrappingLocalBusiness.java 2010-05-19 21:54:20 UTC (rev 105003)
+++ projects/ejb-book/trunk/testsupport/src/main/java/org/jboss/ejb3/examples/testsupport/txwrap/TxWrappingLocalBusiness.java 2010-05-19 22:20:38 UTC (rev 105004)
@@ -38,6 +38,15 @@
//-------------------------------------------------------------------------------------||
/**
+ * Wraps the specified task in a new Transaction, returning the value
+ *
+ * @param task
+ * @throws IllegalArgumentException If no task is specified
+ * @throws TaskExecutionException If an error occurred in invoking {@link Callable#call()}
+ */
+ <T> T wrapInTx(Callable<T> task) throws IllegalArgumentException, TaskExecutionException;
+
+ /**
* Wraps the specified tasks in a new Transaction
*
* @param task
More information about the jboss-cvs-commits
mailing list