EJB中entityBean

用jdeveloper 开发 ejb时,的确很方便.但是,在创建entitybean时,一定要注意一点,这点是我经常犯的错误.说明如下:

开发工具 jdeveloper
数据库 Oracle
创建数据库脚本 
create table myinfo(
    usernum varchar2(20),
    username varchar2(20)
)

我们用这段脚本创建好数据库,并生成实体bean.
下一步我们将entitybean部署,但是他会jdeveloper 会抛出这样的异常

//异常/
[EJB 3.0]: Assigning default-data-source=jdbc/tonywolfDS, specified in projects EJB 3.0 properties
[Starting OC4J using the following ports: HTTP=8990, RMI=23893, JMS=9229.]
F:/jdevstudio10132/jdev/system/oracle.j2ee.10.1.3.40.66/embedded-oc4j/config>
F:/jdevstudio10132/jdk/bin/javaw.exe -client -classpath F:/jdevstudio10132/j2ee/home/oc4j.jar;F:/jdevstudio10132/jdev/lib/jdev-oc4j-embedded.jar -Xverify:none -XX:MaxPermSize=256m -DcheckForUpdates=adminClientOnly -Doracle.application.environment=development -Doracle.j2ee.dont.use.memory.archive=true -Doracle.j2ee.http.socket.timeout=500 -Doc4j.jms.usePersistenceLockFiles=false oracle.oc4j.loader.boot.BootStrap -config F:/jdevstudio10132/jdev/system/oracle.j2ee.10.1.3.40.66/embedded-oc4j/config/server.xml
[waiting for the server to complete its initialization...]
2009-5-9 9:22:26 com.evermind.server.jms.JMSMessages log
信息: JMSServer[]: OC4J JMS server recovering transactions (commit 0) (rollback 0) (prepared 0).
2009-5-9 9:22:26 com.evermind.server.jms.JMSMessages log
信息: JMSServer[]: OC4J JMS server recovering local transactions Queue[jms/Oc4jJmsExceptionQueue].
[TopLink Config]: 2009.05.09 09:22:28.703--ServerSession(31528250)--The alias name for the entity class [class project1.Bonus] is being defaulted to: Bonus.
[TopLink Config]: 2009.05.09 09:22:28.875--ServerSession(31528250)--The table name for entity [class project1.Bonus] is being defaulted to: BONUS.
[TopLink Config]: 2009.05.09 09:22:28.875--ServerSession(31528250)--The column name for element [public java.lang.Long project1.Bonus.getSal()] is being defaulted to: SAL.
[TopLink Config]: 2009.05.09 09:22:28.890--ServerSession(31528250)--The column name for element [public java.lang.String project1.Bonus.getJob()] is being defaulted to: JOB.
[TopLink Config]: 2009.05.09 09:22:28.890--ServerSession(31528250)--The column name for element [public java.lang.String project1.Bonus.getEname()] is being defaulted to: ENAME.
[TopLink Config]: 2009.05.09 09:22:28.890--ServerSession(31528250)--The column name for element [public java.lang.Long project1.Bonus.getComm()] is being defaulted to: COMM.
2009-5-9 9:22:28 com.evermind.server.ejb.logging.EJBMessages logException
严重: [current-workspace-app] 部署 EJB 模块时出错: com.evermind.server.ejb.exception.DeploymentException: [current-workspace-app:Application2_Project1_0] - 使用 PersistenceProvider 类 oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider 为持久性单元 Project1 创建 EntityManagerFactory 时出现异常错误。
com.evermind.server.ejb.exception.DeploymentException: [current-workspace-app:Application2_Project1_0] - 使用 PersistenceProvider 类 oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider 为持久性单元 Project1 创建 EntityManagerFactory 时出现异常错误。
    at com.evermind.server.ejb.exception.DeploymentException.exceptionCreatingEntityManagerFactory(DeploymentException.java:130)
    at com.evermind.server.ejb.persistence.PersistenceUnitManagerImpl.createContainerEntityManagerFactory(PersistenceUnitManagerImpl.java:197)
    at com.evermind.server.ejb.persistence.PersistenceUnitManagerImpl.initializePersistenceUnit(PersistenceUnitManagerImpl.java:159)
    at com.evermind.server.ejb.persistence.PersistenceUnitManagerImpl.initialize(PersistenceUnitManagerImpl.java:86)
    at com.evermind.server.ejb.EJBPackageDeployment.initializePersistenceUnitManager(EJBPackageDeployment.java:1022)
    at com.evermind.server.ejb.EJBContainer.postInit(EJBContainer.java:845)
    at com.evermind.server.ApplicationStateRunning.initializeApplication(ApplicationStateRunning.java:217)
    at com.evermind.server.Application.setConfig(Application.java:439)
    at com.evermind.server.Application.setConfig(Application.java:340)
    at com.evermind.server.ApplicationServer.addApplication(ApplicationServer.java:1853)
    at com.evermind.server.ApplicationServer.initializeDeployedApplications(ApplicationServer.java:1608)
    at com.evermind.server.ApplicationServer.setConfig(ApplicationServer.java:990)
    at com.evermind.server.ApplicationServerLauncher.run(ApplicationServerLauncher.java:131)
    at java.lang.Thread.run(Thread.java:595)
Caused by: Exception [TOPLINK-7161] (Oracle TopLink Essentials - 2006.8 (Build 060829)): oracle.toplink.essentials.exceptions.ValidationException
Exception Description: Entity class [class project1.Bonus] has no primary key specified. It should define either an @Id, @EmbeddedId or an @IdClass.
    at oracle.toplink.essentials.exceptions.ValidationException.noPrimaryKeyAnnotationsFound(ValidationException.java:1112)
    at oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataValidator.throwNoPrimaryKeyAnnotationsFound(MetadataValidator.java:210)
    at oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.ClassAccessor.validatePrimaryKey(ClassAccessor.java:1470)
    at oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.ClassAccessor.process(ClassAccessor.java:463)
    at oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataProcessor.processAnnotations(MetadataProcessor.java:196)
    at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl.processORMetadata(EntityManagerSetupImpl.java:993)
    at oracle.toplink.essentials.internal.ejb.cmp3.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:501)
    at oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider.createContainerEntityManagerFactory(EntityManagerFactoryProvider.java:152)
    at com.evermind.server.ejb.persistence.PersistenceUnitManagerImpl.createContainerEntityManagerFactory(PersistenceUnitManagerImpl.java:192)
    ... 12 more
09/05/09 09:22:28 警告: Application.setConfig Application: current-workspace-app is in failed state as initialization failed.
java.lang.InstantiationException: Error initializing ejb-modules: [current-workspace-app:Application2_Project1_0] - 使用 PersistenceProvider 类 oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider 为持久性单元 Project1 创建 EntityManagerFactory 时出现异常错误。
2009-5-9 9:22:28 com.evermind.server.ServerMessages warningApplicationInitializationFailed
警告: 初始化已部署的应用程序时出现异常错误: current-workspace-app。Application: current-workspace-app is in failed state as initialization failed
Ready message received from Oc4jNotifier.
Embedded OC4J startup time: 7187 ms.
09/05/09 09:22:30 Oracle Containers for J2EE 10g (10.1.3.1.1)  initialized

//
原因,因为我们在部署entitybean的时候需要有主件的支持,但是刚才的sql脚本里却不含主键,因此,会抛出这样的异常.
解决办法, 设置主键即可


EJB(Enterprise JavaBeans),Session Bean 和 Entity Bean 是两种不同类型的组件,用于实现不同的功能。 1. Session Bean(会话Bean): Session Bean 是用于处理业务逻辑的组件。它代表了一个特定的会话,可以执行一系列相关的操作。Session Bean 可以分为以下两种类型: - Stateless Session Bean(无状态会话Bean):它不保存客户端的状态信息,每个方法调用都是独立的。适用于无需保存客户端状态的业务逻辑。 - Stateful Session Bean(有状态会话Bean):它保存了客户端的状态信息,可以跟踪多个方法调用之间的状态。适用于需要保存客户端状态的业务逻辑。 2. Entity Bean(实体Bean): Entity Bean 用于表示业务领域的实体对象,例如数据库表的行。它们持久化到数据库,并提供对这些实体对象的增删改查等操作。Entity Bean 可以分为以下两种类型: - Container-Managed Persistence(CMP):容器管理持久化。在 CMP ,容器负责管理实体的持久化,开发人员只需定义实体类和相关的映射关系。 - Bean-Managed Persistence(BMP):Bean管理持久化。在 BMP ,开发人员需要自己编写代码来控制实体的持久化和数据库访问。 区别和含义: - Session Bean 主要用于处理业务逻辑,不负责持久化数据,而 Entity Bean 用于表示实体对象,并负责将实体对象持久化到数据库。 - Session Bean 可以是无状态或有状态的,而 Entity Bean 通常是有状态的,因为它们需要维护实体对象的状态和标识。 - Session Bean 的生命周期由客户端决定,而 Entity Bean 的生命周期由容器管理。 - CMP 提供了更简单的持久化解决方案,开发人员无需编写 SQL 或 JDBC 代码,而 BMP 需要开发人员手动管理数据库访问和事务。 需要注意的是,EJB 在 Java EE 6 版本之后已经进入了轻量级的 CDI(Contexts and Dependency Injection)时代,推荐使用 CDI 来管理会话和实体等组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值