SoFunction
Updated on 2025-03-09

Inheritance of EJB3.0

In EJB3.0, entity beans can implement inheritance relationships. For example, there is a Person entity bean, which has two attributes: name and gender.

When God and Nuwa created man, they created two kinds of human beings: Man and Woman. Both Man and Woman are physical beans, and they both inherit Person.

A single table strategy means that the data of sub-entities and sub-entities are stored in a table, and a column is specified to distinguish these entities.

like:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE, discriminatorType = )
@DiscriminatorColumn(name = "P_TYPE", nullable = true)
@Inheritance's comment statement is as follows:
@ @Target({TYPE}) @Retention(RUNTIME)

public @interface Inheritance {
InheritanceType strategy() default SINGLE_TABLE;
DiscriminatorType discriminatorType() default STRING;
String discriminatorValue() default "";
}

This comment is used to specify the policy used by inheritance and to distinguish the types and values ​​of the columns used by these entities.

@DiscriminatorColumn annotation is used on single table policies and joint table policies. Used to specify the columns required to distinguish each entity.
@Target({TYPE}) @Retention(RUNTIME)

public @interface DiscriminatorColumn {
String name() default "";
boolean nullable() default false;
String columnDefinition() default "";
int length() default 10;
}

This example mainly contains the following documents. This example mainly implements the inheritance relationship between Person and Man and Woman. The examples introduced in the following two chapters are the same as this example. Man and Woman inherit the Person entity bean. As in the previous example, we still use Client tests.

: Entity Bean.

: The class on which entity beans depend.

: The class on which entity beans depend.

: The service interface of the session bean

: The implementation class of session bean

: Test EJB's client class.

: jndi attribute file, providing access to the basic configuration properties of jdni.

:ant configuration file to compile, publish, test, and clear EJB.

The following is an introduction to the content of each file.



package .;

import ;
import ;
import ;
import ;
import ;
import ;
import ;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE, discriminatorType = )
@DiscriminatorColumn(name = "P_TYPE", nullable = true)
public class Person implements
{
private int id;
private String name;
private String gender;

@Id(generate = )

public int getId()
{
return id;
}

public void setId(int id)
{
= id;
}

public String getName()
{
return name;
}

public void setName(String name)
{
= name;
}

public void setGender(String gender)
{
= gender;
}

public String getGender()
{
return gender;
}

}

Specifies that the P_TYPE column is used to distinguish the individual beans.



package .;

import ;
import ;
import ;
import ;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE, discriminatorType = , discriminatorValue = "Man")

public class Man extends Person
{
private boolean isGood;
public void setGood(boolean isGood)
{
= isGood;
}

public boolean isGood()
{
return isGood;
}
}

This entity bean adds a property of whether it is a good man.



package .;

import ;
import ;
import ;
import ;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE, discriminatorType = , discriminatorValue = "Woman")

public class Woman extends Person
{
private boolean isbeautiful;

public void setIsbeautiful(boolean isbeautiful)
{
= isbeautiful;
}

public boolean isIsbeautiful()
{
return isbeautiful;
}

}



package .;

import ;
import ;

@Remote

public interface PersonDAO
{
public int createMan(String name,String gender,boolean b);
public int createWoman(String name,String gender,boolean b);
public Person find(int i);
public List findByName(String name);
public List findByInfo(String gender);
}



package .;

import ;
import ;
import ;
import ;

@Stateless

public class PersonDAOBean implements PersonDAO
{
@Inject
private EntityManager manager;

public int createMan(String name,String gender,boolean b)
{
Man man = new Man();
(name);
(gender);
(b);
(man);
return ();
}

public int createWoman(String name, String gender,boolean b)
{
Woman woman = new Woman();
(name);
(gender);
(b);
(woman);
return ();
}

public Person find(int i)
{
return (,i);
}

public List findByName(String name)
{
return ("from Person p where =:name").setParameter("name", name).listResults();
}

public List findByInfo(String gender)
{
return ("from Person p where =:gender").setParameter("gender", gender).listResults();
}

}

This session bean provides a method to create Man and Woman entity beans, and provides a search method.



package .;

import ;
import ;
import ;

public class Client
{
public static void main(String[] args) throws NamingException
{
InitialContext ctx = new InitialContext();
PersonDAO dao = (PersonDAO) (());
int i = ("Chao Yuepan","Male",true);
("Zhu Lihuan","female",true);
Person p = (i);
("%s' gender: %s%n",(),());
List list = ("Zhu Lihuan");

for (Object o:list)
{
 Woman w = (Woman)o;
("%s is beautiful? Conclusion: %b%n",(),());
}
}

}

This client is used for testing.

Please run: run –c all in the bin directory and start JBOSS.

http://localhost:8080/jmx-console/HtmlAdaptor?action=inspectMBean&name=jboss%3Aservice%3DHypersonic%2Cdatabase%3DlocalDB, then call the startDatabaseManager() method to open the HSQL management tool to manage the database.

Execute ejbjar target in the Ant view of Eclipse. Or on the command line, enter this project directory, execute ant ejbjar, and publish this EJB with a package.

Execute a run target in the Ant view of Eclipse. Or on the command line, enter this project directory, execute ant run, and test this EJB.