package org.superbiz.inheritance;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Inheritance;
import jakarta.persistence.InheritanceType;
import java.io.Serializable;
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Item implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String brand;
private String itemName;
private double price;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
Webservice Inheritance
Help us document this example! Click the blue pencil icon in the upper right to edit this page.
Item
Tower
package org.superbiz.inheritance;
import jakarta.persistence.Entity;
@Entity
public class Tower extends Item {
private Fit fit;
private String tubing;
public static enum Fit {
Custom, Exact, Universal
}
public Fit getFit() {
return fit;
}
public void setFit(Fit fit) {
this.fit = fit;
}
public String getTubing() {
return tubing;
}
public void setTubing(String tubing) {
this.tubing = tubing;
}
;
}
Wakeboard
package org.superbiz.inheritance;
import jakarta.persistence.Entity;
@Entity
public class Wakeboard extends Wearable {
}
WakeboardBinding
package org.superbiz.inheritance;
import jakarta.persistence.Entity;
@Entity
public class WakeboardBinding extends Wearable {
}
WakeRiderImpl
package org.superbiz.inheritance;
import jakarta.ejb.Stateless;
import jakarta.jws.WebService;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.PersistenceContextType;
import jakarta.persistence.Query;
import java.util.List;
/**
* This is an EJB 3 style pojo stateless session bean Every stateless session
* bean implementation must be annotated using the annotation @Stateless This
* EJB has a single interface: {@link WakeRiderWs} a webservice interface.
*/
@Stateless
@WebService(
portName = "InheritancePort",
serviceName = "InheritanceWsService",
targetNamespace = "http://superbiz.org/wsdl",
endpointInterface = "org.superbiz.inheritance.WakeRiderWs")
public class WakeRiderImpl implements WakeRiderWs {
@PersistenceContext(unitName = "wakeboard-unit", type = PersistenceContextType.TRANSACTION)
private EntityManager entityManager;
public void addItem(Item item) throws Exception {
entityManager.persist(item);
}
public void deleteMovie(Item item) throws Exception {
entityManager.remove(item);
}
public List<Item> getItems() throws Exception {
Query query = entityManager.createQuery("SELECT i FROM Item i");
List<Item> items = query.getResultList();
return items;
}
}
WakeRiderWs
package org.superbiz.inheritance;
import jakarta.jws.WebService;
import jakarta.xml.bind.annotation.XmlSeeAlso;
import java.util.List;
/**
* This is an EJB 3 webservice interface that uses inheritance.
*/
@WebService(targetNamespace = "http://superbiz.org/wsdl")
@XmlSeeAlso({Wakeboard.class, WakeboardBinding.class, Tower.class})
public interface WakeRiderWs {
public void addItem(Item item) throws Exception;
public void deleteMovie(Item item) throws Exception;
public List<Item> getItems() throws Exception;
}
Wearable
package org.superbiz.inheritance;
import jakarta.persistence.MappedSuperclass;
@MappedSuperclass
public abstract class Wearable extends Item {
protected String size;
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
}
ejb-jar.xml
<ejb-jar/>
persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="wakeboard-unit">
<jta-data-source>wakeBoardDatabase</jta-data-source>
<non-jta-data-source>wakeBoardDatabaseUnmanaged</non-jta-data-source>
<class>org.superbiz.inheritance.Item</class>
<class>org.superbiz.inheritance.Tower</class>
<class>org.superbiz.inheritance.Wakeboard</class>
<class>org.superbiz.inheritance.WakeboardBinding</class>
<class>org.superbiz.inheritance.Wearable</class>
<properties>
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
</properties>
</persistence-unit>
</persistence>
InheritanceTest
package org.superbiz.inheritance;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.superbiz.inheritance.Tower.Fit;
import javax.xml.namespace.QName;
import jakarta.xml.ws.Service;
import java.net.URL;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@RunWith(Arquillian.class)
public class InheritanceTest {
//START SNIPPET: setup
@ArquillianResource
private URL base;
@Deployment(testable = false)
public static WebArchive war() {
return ShrinkWrap.create(WebArchive.class, "webservice-inheritance.war")
.addClasses(Item.class, Tower.class, Wakeboard.class, WakeboardBinding.class, Wearable.class,
WakeRiderImpl.class, WakeRiderWs.class)
.addAsResource("META-INF/persistence.xml");
}
//END SNIPPET: setup
/**
* Create a webservice client using wsdl url
*
* @throws Exception
*/
//START SNIPPET: webservice
@Test
public void testInheritanceViaWsInterface() throws Exception {
Service service = Service.create(
new URL(base.toExternalForm() + "webservices/WakeRiderImpl?wsdl"),
new QName("http://superbiz.org/wsdl", "InheritanceWsService"));
assertNotNull(service);
WakeRiderWs ws = service.getPort(WakeRiderWs.class);
Tower tower = createTower();
Item item = createItem();
Wakeboard wakeBoard = createWakeBoard();
WakeboardBinding wakeBoardbinding = createWakeboardBinding();
ws.addItem(tower);
ws.addItem(item);
ws.addItem(wakeBoard);
ws.addItem(wakeBoardbinding);
List<Item> returnedItems = ws.getItems();
assertEquals("testInheritanceViaWsInterface, nb Items", 4, returnedItems.size());
//check tower
assertEquals("testInheritanceViaWsInterface, first Item", returnedItems.get(0).getClass(), Tower.class);
tower = (Tower) returnedItems.get(0);
assertEquals("testInheritanceViaWsInterface, first Item", tower.getBrand(), "Tower brand");
assertEquals("testInheritanceViaWsInterface, first Item", tower.getFit().ordinal(), Fit.Custom.ordinal());
assertEquals("testInheritanceViaWsInterface, first Item", tower.getItemName(), "Tower item name");
assertEquals("testInheritanceViaWsInterface, first Item", tower.getPrice(), 1.0d, 0.0d);
assertEquals("testInheritanceViaWsInterface, first Item", tower.getTubing(), "Tower tubing");
//check item
assertEquals("testInheritanceViaWsInterface, second Item", returnedItems.get(1).getClass(), Item.class);
item = (Item) returnedItems.get(1);
assertEquals("testInheritanceViaWsInterface, second Item", item.getBrand(), "Item brand");
assertEquals("testInheritanceViaWsInterface, second Item", item.getItemName(), "Item name");
assertEquals("testInheritanceViaWsInterface, second Item", item.getPrice(), 2.0d, 0.0d);
//check wakeboard
assertEquals("testInheritanceViaWsInterface, third Item", returnedItems.get(2).getClass(), Wakeboard.class);
wakeBoard = (Wakeboard) returnedItems.get(2);
assertEquals("testInheritanceViaWsInterface, third Item", wakeBoard.getBrand(), "Wakeboard brand");
assertEquals("testInheritanceViaWsInterface, third Item", wakeBoard.getItemName(), "Wakeboard item name");
assertEquals("testInheritanceViaWsInterface, third Item", wakeBoard.getPrice(), 3.0d, 0.0d);
assertEquals("testInheritanceViaWsInterface, third Item", wakeBoard.getSize(), "WakeBoard size");
//check wakeboardbinding
assertEquals("testInheritanceViaWsInterface, fourth Item", returnedItems.get(3).getClass(), WakeboardBinding.class);
wakeBoardbinding = (WakeboardBinding) returnedItems.get(3);
assertEquals("testInheritanceViaWsInterface, fourth Item", wakeBoardbinding.getBrand(), "Wakeboardbinding brand");
assertEquals("testInheritanceViaWsInterface, fourth Item", wakeBoardbinding.getItemName(), "Wakeboardbinding item name");
assertEquals("testInheritanceViaWsInterface, fourth Item", wakeBoardbinding.getPrice(), 4.0d, 0.0d);
assertEquals("testInheritanceViaWsInterface, fourth Item", wakeBoardbinding.getSize(), "WakeBoardbinding size");
}
//END SNIPPET: webservice
private Tower createTower() {
Tower tower = new Tower();
tower.setBrand("Tower brand");
tower.setFit(Fit.Custom);
tower.setItemName("Tower item name");
tower.setPrice(1.0f);
tower.setTubing("Tower tubing");
return tower;
}
private Item createItem() {
Item item = new Item();
item.setBrand("Item brand");
item.setItemName("Item name");
item.setPrice(2.0f);
return item;
}
private Wakeboard createWakeBoard() {
Wakeboard wakeBoard = new Wakeboard();
wakeBoard.setBrand("Wakeboard brand");
wakeBoard.setItemName("Wakeboard item name");
wakeBoard.setPrice(3.0f);
wakeBoard.setSize("WakeBoard size");
return wakeBoard;
}
private WakeboardBinding createWakeboardBinding() {
WakeboardBinding wakeBoardBinding = new WakeboardBinding();
wakeBoardBinding.setBrand("Wakeboardbinding brand");
wakeBoardBinding.setItemName("Wakeboardbinding item name");
wakeBoardBinding.setPrice(4.0f);
wakeBoardBinding.setSize("WakeBoardbinding size");
return wakeBoardBinding;
}
}
arquillian.xml
The test deploys the example to TomEE embedded. src/test/resources/arquillian.xml defines the data sources the persistence unit uses:
<arquillian
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>
<container qualifier="tomee" default="true">
<configuration>
<property name="httpPort">-1</property>
<property name="stopPort">-1</property>
<property name="appWorkingDir">target/tomee-embedded</property>
<property name="properties">
wakeBoardDatabase = new://Resource?type=DataSource
wakeBoardDatabase.JdbcDriver = org.hsqldb.jdbcDriver
wakeBoardDatabase.JdbcUrl = jdbc:hsqldb:mem:wakeBoarddb
wakeBoardDatabaseUnmanaged = new://Resource?type=DataSource
wakeBoardDatabaseUnmanaged.JdbcDriver = org.hsqldb.jdbcDriver
wakeBoardDatabaseUnmanaged.JdbcUrl = jdbc:hsqldb:mem:wakeBoarddb
wakeBoardDatabaseUnmanaged.JtaManaged = false
</property>
</configuration>
</container>
</arquillian>
Running
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.superbiz.inheritance.InheritanceTest
INFO: Starting Servlet engine: [Apache Tomcat/11.0.25]
INFO: Version: 10.3.0-SNAPSHOT
INFO: Configuring Service(id=wakeBoardDatabase, type=Resource, provider-id=Default JDBC Database)
INFO: Configuring Service(id=wakeBoardDatabaseUnmanaged, type=Resource, provider-id=Default JDBC Database)
INFO: Creating Resource(id=wakeBoardDatabase)
INFO: Creating Resource(id=wakeBoardDatabaseUnmanaged)
INFO: ------------------------- localhost -> /webservice-inheritance
INFO: Configuring enterprise application: /path/to/examples/webservice-inheritance/target/tomee-embedded/0/webservice-inheritance
INFO: Auto-deploying ejb WakeRiderImpl: EjbDeployment(deployment-id=WakeRiderImpl)
INFO: Created Ejb(deployment-id=WakeRiderImpl, ejb-name=WakeRiderImpl, container=Default Stateless Container)
INFO: Started Ejb(deployment-id=WakeRiderImpl, ejb-name=WakeRiderImpl, container=Default Stateless Container)
INFO: Webservice(wsdl=http://localhost:53482/webservice-inheritance/webservices/WakeRiderImpl, qname={http://superbiz.org/wsdl}InheritanceWsService) --> Ejb(id=WakeRiderImpl)
INFO: Deployed Application(path=/path/to/examples/webservice-inheritance/target/tomee-embedded/0/webservice-inheritance)
INFO: Creating Service {http://superbiz.org/wsdl}InheritanceWsService from WSDL: http://localhost:53482/webservice-inheritance/webservices/WakeRiderImpl?wsdl
INFO: Undeploying app: /path/to/examples/webservice-inheritance/target/tomee-embedded/0/webservice-inheritance
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.792 s -- in org.superbiz.inheritance.InheritanceTest
Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0