Khi làm việc với Ejb có thể bạn sẽ gặp lỗi

Caused by: java.lang.IllegalArgumentException: Object: com.abcd.database.model.Device[ id=null ] is not a known entity type.

Lỗi sau đây khiến bạn gặp thông báo như trên và cách xử lý nó:

Chưa khai báo thực thể Persistence trong persistence.xml

Trong file persistence.xml bạn sẽ phải khai báo chi tiết các đối tượng Persistence như sau:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="com.abcd_abcd-database-ejb_jar_1.0-SNAPSHOTPU" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

        <jta-data-source>jdbc/abcdDatasource</jta-data-source>
        <class>com.abcd.database.model.Performance</class>
        <class>com.abcd.database.model.AlarmHistory</class>
        <class>com.abcd.database.model.Device</class>
        <class>com.abcd.database.model.Firmware</class>
        <class>com.abcd.database.model.Area</class>
        <class>com.abcd.database.model.Alarm</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
        <properties>
            <!-- EclipseLink properties -->
            <property name="eclipselink.target-database" value="MySQL" />
            <property name="eclipselink.logging.level" value="INFO" />
            <property name="eclipselink.ddl-generation" value="none" />
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
        </properties>
    </persistence-unit>
</persistence>

Hoặc đơn giản không cần khai báo chi tiết các đối tượng Persistence, bạn chỉ cần sửa lại file persistence.xml bằng cách thay đổi dòng 6 như sau

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="com.abcd_abcd-database-ejb_jar_1.0-SNAPSHOTPU" transaction-type="JTA">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

        <jta-data-source>jdbc/abcdDatasource</jta-data-source>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>
            <!-- EclipseLink properties -->
            <property name="eclipselink.target-database" value="MySQL" />
            <property name="eclipselink.logging.level" value="INFO" />
            <property name="eclipselink.ddl-generation" value="none" />
            <property name="eclipselink.ddl-generation.output-mode" value="database" />
        </properties>
    </persistence-unit>
</persistence>

Thông báo thành công hợp lệ

Để chắc chắn các đối tượng Persistence được nhận dạng đúng và hợp lệ, trên giao diện log console, bạn sẽ nhìn thấy thông báo hiển thị các model như sau:

Config:   The access type for the persistent class [class com.abcd.database.model.Device] is set to [FIELD].
Config:   The target entity (reference) class for the many to one mapping element [field areaId] is being defaulted to: class com.abcd.database.model.Area.
Config:   The access type for the persistent class [class com.abcd.database.model.Performance] is set to [FIELD].
Config:   The access type for the persistent class [class com.abcd.database.model.Firmware] is set to [FIELD].
Config:   The access type for the persistent class [class com.abcd.database.model.AlarmHistory] is set to [FIELD].
Config:   The access type for the persistent class [class com.abcd.database.model.Alarm] is set to [FIELD].
Config:   The access type for the persistent class [class com.abcd.database.model.Area] is set to [FIELD].
Config:   The target entity (reference) class for the one to many mapping element [field deviceList] is being defaulted to: class com.abcd.database.model.Device.
Config:   The alias name for the entity class [class com.abcd.database.model.Device] is being defaulted to: Device.
Config:   The alias name for the entity class [class com.abcd.database.model.Performance] is being defaulted to: Performance.
Config:   The alias name for the entity class [class com.abcd.database.model.Firmware] is being defaulted to: Firmware.
Config:   The alias name for the entity class [class com.abcd.database.model.AlarmHistory] is being defaulted to: AlarmHistory.
Config:   The alias name for the entity class [class com.abcd.database.model.Alarm] is being defaulted to: Alarm.
Config:   The alias name for the entity class [class com.abcd.database.model.Area] is being defaulted to: Area.
Info:   JTS5014: Recoverable JTS instance, serverId = [3700]
Info:   Portable JNDI names for EJB PerformanceFacade: [java:global/abcd-database-ejb/PerformanceFacade!com.abcd.database.ejb.PerformanceFacadeRemote, java:global/abcd-database-ejb/PerformanceFacade]
Info:   Glassfish-specific (Non-portable) JNDI names for EJB PerformanceFacade: [com.abcd.database.ejb.PerformanceFacadeRemote#com.abcd.database.ejb.PerformanceFacadeRemote, com.abcd.database.ejb.PerformanceFacadeRemote]
Info:   Portable JNDI names for EJB DeviceFacade: [java:global/abcd-database-ejb/DeviceFacade, java:global/abcd-database-ejb/DeviceFacade!com.abcd.database.ejb.DeviceFacadeRemote]
Info:   Glassfish-specific (Non-portable) JNDI names for EJB DeviceFacade: [com.abcd.database.ejb.DeviceFacadeRemote#com.abcd.database.ejb.DeviceFacadeRemote, com.abcd.database.ejb.DeviceFacadeRemote]
Info:   Portable JNDI names for EJB AlarmFacade: [java:global/abcd-database-ejb/AlarmFacade, java:global/abcd-database-ejb/AlarmFacade!com.abcd.database.ejb.AlarmFacadeRemote]
Info:   Glassfish-specific (Non-portable) JNDI names for EJB AlarmFacade: [com.abcd.database.ejb.AlarmFacadeRemote, com.abcd.database.ejb.AlarmFacadeRemote#com.abcd.database.ejb.AlarmFacadeRemote]

Trường hợp bạn đã chắc chắn mình làm rất đúng rồi nhưng vẫn nhận được thông báo Object: com.abcd.database.model.Device[ id=null ] is not a known entity type.! Hãy xóa cache của IDE hoặc Restart lại máy chủ Glassfish Server.

Chúc bạn thành công !

About The Author