EJB3.0之实体Bean的继承_MySQL
EJB
在EJB3.0中,实体Bean可以实现继承关系。 比如有个Person的实体bean,它有姓名和性别两个属性。
上帝和女娲造人的时候,造出两种人:Man和Woman。Man和Woman都是实体Bean,而且他们都继承Person。
单一表策略就是副实体和子实体的数据都存放在一张表中,同时指定一列用来区别这些实体。
如:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE, discriminatorType = DiscriminatorType.STRING)
@DiscriminatorColumn(name = "P_TYPE", nullable = true)
@ Inheritance的注释声明如下:
@ @Target({TYPE}) @Retention(RUNTIME)
public @interface Inheritance {
InheritanceType strategy() default SINGLE_TABLE;
DiscriminatorType discriminatorType() default STRING;
String discriminatorValue() default "";
}
这个注释用来指定继承使用的策略,以及为了区别这些实体所用的列的类型与值。
@DiscriminatorColumn注释用在单一表策略和联合表策略上。用来指定区别各实体所需的列。
@Target({TYPE}) @Retention(RUNTIME)
public @interface DiscriminatorColumn {
String name() default "";
boolean nullable() default false;
String columnDefinition() default "";
int length() default 10;
}
这个例子主要有以下几个文件,这个例子主要实现了Person和Man、Woman之间的继承关系,下面两章介绍的例子和这个例子相同。Man和Woman继承Person实体Bean。前面的例子一样,我们还是使用Client测试。
Person.java:实体Bean。
Man.java:实体Bean所依赖的类。
Woman.java:实体Bean所依赖的类。
PersonTest.java:会话Bean的业务接口
PersonTestBean.java:会话Bean的实现类
Client.java:测试EJB的客户端类。
jndi.properties:jndi属性文件,提供访问jdni的基本配置属性。
Build.xml:ant 配置文件,用以编译、发布、测试、清除EJB。
下面针对每个文件的内容做一个介绍。
Person.java
package com.kuaff.ejb3.singleinheritance;
import javax.ejb.DiscriminatorColumn;
import javax.ejb.DiscriminatorType;
import javax.ejb.Entity;
import javax.ejb.GeneratorType;
import javax.ejb.Id;
import javax.ejb.Inheritance;
import javax.ejb.InheritanceType;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE, discriminatorType = DiscriminatorType.STRING)
@DiscriminatorColumn(name = "P_TYPE", nullable = true)
public class Person implements java.io.Serializable
{
private int id;
private String name;
private String gender;
@Id(generate = GeneratorType.AUTO)
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public void setGender(String gender)
{
this.gender = gender;
}
public String getGender()
{
return gender;
}
}
指定使用P_TYPE列用来区别各实体Bean。
Man.java
package com.kuaff.ejb3.singleinheritance;
import javax.ejb.DiscriminatorType;
import javax.ejb.Entity;
import javax.ejb.Inheritance;
import javax.ejb.InheritanceType;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE, discriminatorType = DiscriminatorType.STRING, discriminatorValue = "Man")
public class Man extends Person
{
private boolean isGood;
public void setGood(boolean isGood)
{
this.isGood = isGood;
}
public boolean isGood()
{
return isGood;
}
}
这个实体Bean增加了一个是否是好男人的属性。
Woman.java
package com.kuaff.ejb3.singleinheritance;
import javax.ejb.DiscriminatorType;
import javax.ejb.Entity;
import javax.ejb.Inheritance;
import javax.ejb.InheritanceType;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE, discriminatorType = DiscriminatorType.STRING, discriminatorValue = "Woman")
public class Woman extends Person
{
private boolean isbeautiful;
public void setIsbeautiful(boolean isbeautiful)
{
this.isbeautiful = isbeautiful;
}
public boolean isIsbeautiful()
{
return isbeautiful;
}
}
EntityTest.java
package com.kuaff.ejb3.singleinheritance;
import javax.ejb.Remote;
import java.util.List;
@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);
}
PersonTestBean.java
package com.kuaff.ejb3.singleinheritance;
import javax.ejb.EntityManager;
import javax.ejb.Inject;
import javax.ejb.Stateless;
import java.util.List;
@Stateless
public class PersonDAOBean implements PersonDAO
{
@Inject
private EntityManager manager;
public int createMan(String name,String gender,boolean b)
{
Man man = new Man();
man.setName(name);
man.setGender(gender);
man.setGood(b);
manager.create(man);
return man.getId();
}
public int createWoman(String name, String gender,boolean b)
{
Woman woman = new Woman();
woman.setName(name);
woman.setGender(gender);
woman.setIsbeautiful(b);
manager.create(woman);
return woman.getId();
}
public Person find(int i)
{
return manager.find(Person.class,i);
}
public List findByName(String name)
{
return manager.createQuery("from Person p where p.name =:name").setParameter("name", name).listResults();
}
public List findByInfo(String gender)
{
return manager.createQuery("from Person p where p.gender =:gender").setParameter("gender", gender).listResults();
}
}
在这个会话Bean中提供了创建Man、Woman实体Bean的方法,并提供了查找方法。
Client.java
package com.kuaff.ejb3.singleinheritance;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.List;
public class Client
{
public static void main(String[] args) throws NamingException
{
InitialContext ctx = new InitialContext();
PersonDAO dao = (PersonDAO) ctx.lookup(PersonDAO.class.getName());
int i = dao.createMan("晁岳攀","男",true);
dao.createWoman("朱立焕","女",true);
Person p = dao.find(i);
System.out.printf("%s的性别:%s%n",p.getName(),p.getGender());
List list = dao.findByName("朱立焕");
for (Object o:list)
{
Woman w = (Woman)o;
System.out.printf("%s漂亮吗?结论:%b%n",w.getName(),w.isIsbeautiful());
}
}
}
这个客户端用来测试。
请运行{$JBOSS_HOME}/bin目录下的run.bat: run

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In function inheritance, use "base class pointer" and "derived class pointer" to understand the inheritance mechanism: when the base class pointer points to the derived class object, upward transformation is performed and only the base class members are accessed. When a derived class pointer points to a base class object, a downward cast is performed (unsafe) and must be used with caution.

Every year before Apple releases a new major version of iOS and macOS, users can download the beta version several months in advance and experience it first. Since the software is used by both the public and developers, Apple has launched developer and public versions, which are public beta versions of the developer beta version, for both. What is the difference between the developer version and the public version of iOS? Literally speaking, the developer version is a developer test version, and the public version is a public test version. The developer version and the public version target different audiences. The developer version is used by Apple for testing by developers. You need an Apple developer account to download and upgrade it.

Inheritance and polymorphism affect the coupling of classes: Inheritance increases coupling because the derived class depends on the base class. Polymorphism reduces coupling because objects can respond to messages in a consistent manner through virtual functions and base class pointers. Best practices include using inheritance sparingly, defining public interfaces, avoiding adding data members to base classes, and decoupling classes through dependency injection. A practical example showing how to use polymorphism and dependency injection to reduce coupling in a bank account application.

Inheritance error debugging tips: Ensure correct inheritance relationships. Use the debugger to step through the code and examine variable values. Make sure to use the virtual modifier correctly. Examine the inheritance diamond problem caused by hidden inheritance. Check for unimplemented pure virtual functions in abstract classes.

What is JPA? How is it different from JDBC? JPA (JavaPersistence API) is a standard interface for object-relational mapping (ORM), which allows Java developers to use familiar Java objects to operate databases without writing SQL queries directly against the database. JDBC (JavaDatabaseConnectivity) is Java's standard API for connecting to databases. It requires developers to use SQL statements to operate the database. JPA encapsulates JDBC, provides a more convenient and higher-level API for object-relational mapping, and simplifies data access operations. In JPA, what is an entity? entity

Encapsulation technology and application encapsulation in PHP is an important concept in object-oriented programming. It refers to encapsulating data and operations on data together in order to provide a unified access interface to external programs. In PHP, encapsulation can be achieved through access control modifiers and class definitions. This article will introduce encapsulation technology in PHP and its application scenarios, and provide some specific code examples. 1. Encapsulated access control modifiers In PHP, encapsulation is mainly achieved through access control modifiers. PHP provides three access control modifiers,

Detailed explanation of C++ function inheritance: Master the relationship between "is-a" and "has-a" What is function inheritance? Function inheritance is a technique in C++ that associates methods defined in a derived class with methods defined in a base class. It allows derived classes to access and override methods of the base class, thereby extending the functionality of the base class. "is-a" and "has-a" relationships In function inheritance, the "is-a" relationship means that the derived class is a subtype of the base class, that is, the derived class "inherits" the characteristics and behavior of the base class. The "has-a" relationship means that the derived class contains a reference or pointer to the base class object, that is, the derived class "owns" the base class object. SyntaxThe following is the syntax for how to implement function inheritance: classDerivedClass:pu

C++ function inheritance should not be used in the following situations: When a derived class requires a different implementation, a new function with a different implementation should be created. When a derived class does not require a function, it should be declared as an empty class or use private, unimplemented base class member functions to disable function inheritance. When functions do not require inheritance, other mechanisms (such as templates) should be used to achieve code reuse.
