EJB
在前面的例子中,我们每一个实体Bean只映射到数据库中的一张表上。事实上,一个实体Bean可以映射到多张表上。在一些需要字典表的项目上会经常用到,象以前我做过的项目,使用到很多国标规定的数据表。在我们下面这个例子中,性别作为一个字典表存在,学生这个实体将映射到学生信息表、性别表这两个表。
从表可以使用@SecondaryTable来注释:
@Target({TYPE}) @Retention(RUNTIME)
public @interface SecondaryTable {
String name();
String catalog() default "";
String schema() default "";
JoinColumn[] join() default {};
UniqueConstraint[] uniqueConstraints() default {};
}
这个注释可以指定表名、分类、schema、联合列、约束等。假如你使用多张表,你可以使用下面的注释来声明多张表:
@SecondaryTable
@Target({TYPE}) @Retention(RUNTIME)
public @interface SecondaryTables {
SecondaryTable[] value() default {};
}
这个例子主要有以下几个文件,这个例子主要实现了管理学生的功能。Student是一个实体Bean,这个Bean的name属性是一个类,也就是Name类,这个Name类就是一个依赖值对象。学生的性别映射到第二张表中。StudentDAOBean是一个无状态的会话Bean,用来调用实体Bean。和前面的例子一样,我们还是使用Client测试。
这个例子和上一个例子基本相同,只是Student.java和Client有所不同。
Student.java:实体Bean。
Name.java:实体Bean所依赖的类。
StudentDAO.java:会话Bean的业务接口
StudentDAOBean.java:会话Bean的实现类
Client.java:测试EJB的客户端类。
jndi.properties:jndi属性文件,提供访问jdni的基本配置属性。
Build.xml:ant 配置文件,用以编译、发布、测试、清除EJB。
下面针对每个文件的内容做一个介绍。
Student.java
package com.kuaff.ejb3.secondary;
import javax.ejb.Dependent;
import javax.ejb.DependentAttribute;
import javax.ejb.Column;
import javax.ejb.Entity;
import javax.ejb.GeneratorType;
import javax.ejb.Id;
import javax.ejb.Table;
import javax.ejb.SecondaryTables;
import javax.ejb.SecondaryTable;
import javax.ejb.JoinColumn;
@Entity
@Table(name = "STUDENT")
@SecondaryTables({
@SecondaryTable(name = "GENDER", join = {@JoinColumn(name = "GENDER_ID")})
})
public class Student implements java.io.Serializable
{
private int id;
private Name name;
private String grade;
private String email;
private String gender;
@Id(generate = GeneratorType.AUTO)
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public void setName(Name name)
{
this.name = name;
}
@Dependent({ @DependentAttribute(name = "first", column ={ @Column(name = "FIRST") }),
@DependentAttribute(name = "last", column ={ @Column(name = "LAST") }) })
public Name getName()
{
return name;
}
public void setGrade(String grade)
{
this.grade = grade;
}
@Column(name = "GRADE")
public String getGrade()
{
return grade;
}
public void setEmail(String email)
{
this.email = email;
}
@Column(name = "EMAIL")
public String getEmail()
{
return email;
}
public void setGender(String gender)
{
this.gender = gender;
}
@Column(name = "gender", secondaryTable = "GENDER")
public String getGender()
{
return gender;
}
}
Student.java实现了Student实体Bean,它提供学生的基本情况。在类上声明上加上了第二张表的注释:
@SecondaryTables({
@SecondaryTable(name = "GENDER", join = {@JoinColumn(name = "GENDER_ID")})
})
在gender属性上加上了映射第二张的注释:
@Column(name = "gender", secondaryTable = "GENDER")
Client.java
package com.kuaff.ejb3.secondary;
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();
StudentDAO dao = (StudentDAO) ctx.lookup(StudentDAO.class.getName());
int id = dao.create("晁","岳攀","8","smallnest@kuaff.com","男");
dao.create("朱","立焕","6","zhuzhu@kuaff.com","女");
List list = dao.findAll();
for(Object o:list)
{
Student s = (Student)o;
System.out.printf("%s%s的性别:%s%n",s.getName().getFirst(),s.getName().getLast(),s.getGender());
dao.evict(s);
}
这个客户端增加学生的分数,并且测试显示这个学生的email。
请运行{$JBOSS_HOME}/bin目录下的run.bat: run

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
