Spring JPA Data - Injecting Dependencies of a Specific Entity Class

We are migrating from MyBatis to Spring Data JPA (using sleep mode). Spring's initial configuration indicated the use of: map of specific domain-level objects as the values ​​referenced by the enum key. Thus, the card was introduced into the service level class. This map was then used to obtain the domain level object based on a specific enumeration during the process flow. Now we have edited the service level to use Spring Domain-level Objects with @Entity data enabled, when we try to get an instance of the object using the enum key, it returns a proxy (as expected). Thus, when we try to use the returned instance, we get a ClassCastException (Caused by: java.lang.ClassCastException: com.sun.proxy. $ Proxy43 cannot be passed to com.ourpackage.Event). My question is: how can I insert the @Entity class into the util: map configuration so that additional properties can be set?

Here is the configuration of the entity object map and enumeration search keys:

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">

    <!--*********************************************************************-->
    <!-- Event bean definitions -->
    <!--*********************************************************************-->
    <bean id="workEvent" class="com.ourpackage.Event">
        <constructor-arg name="action" type="java.lang.String" value="action"/>
        <constructor-arg name="type" type="java.lang.String" value="type"/>
        <constructor-arg name="description" type="java.lang.String" value="A description"/>
    </bean>

    <!--*********************************************************************-->
    <!-- Event Maps -->
    <!--*********************************************************************-->
    <util:map id="workEvents" map-class="java.util.HashMap" key-type="com.anotherpackage.EventType" value-type="com.ourpackage.Event">
        <entry>
            <key><value type="com.anotherpackage.EventType">WORK_ITEM</value></key>
            <ref local="workEvent"/>
        </entry>
    </util:map>
</beans>

And here is the domain class definition:

package com.ourpackage;

import static javax.persistence.GenerationType.IDENTITY;
import static javax.persistence.TemporalType.TIMESTAMP;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.transaction.annotation.Transactional;

import com.basepackage.ServiceEntity;

/**
 * The Class Event.
 *
 */
@Entity
@Table(name = "events")
@Transactional(readOnly = true)
public class Event extends ServiceEntity {
    ....
}

Here is the code from the class of service using this card:

// The following line produces the ClassCastException
workEvent = workEvents.get(EventType.WORK_ITEM);

Of course, this can be solved by breaking the dependency injection configuration and simply creating the Event class on the fly, but the configuration is the preferred option.

Any help would be greatly appreciated.

+4
source share
1 answer

The problem is that the particular class is no longer com.ourpackage.Eventbecause Spring wrapped the type proxy in it com.sun.proxy.$Proxy43, and it is impossible to pass the proxy for the event, because these are two completely different types.

, , , , .

, -/AOP (, Spring/Hibernate).

+3

Source: https://habr.com/ru/post/1524486/


All Articles