使用程序包专用实体

时间:2019-05-03 06:41:43

标签: java hibernate spring-data-jpa

我有一个私有的@Entity类:

package org.github.saphyra.skyxplore.community.blockedcharacter.repository;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Data
@Builder
@Table(name = "blocked_character")
@Entity
@NoArgsConstructor
@AllArgsConstructor
class BlockedCharacterEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name = "blocked_character_entity_id")
    private Long blockedCharacterEntityId;

    @Column(name = "character_id")
    private String characterId;

    @Column(name = "blocked_character_id")
    private String blockedCharacterId;
}

具有程序包专用的Jpa存储库:

package org.github.saphyra.skyxplore.community.blockedcharacter.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
//TODO unit test
public interface BlockedCharacterRepository extends JpaRepository<BlockedCharacterEntity, Long> {
    @Modifying
    @Transactional
    @Query("DELETE BlockedCharacterEntity b WHERE b.characterId = :characterId OR b.blockedCharacterId = :characterId")
    void deleteByCharacterId(@Param("characterId") String characterId);

    List<BlockedCharacterEntity> findByCharacterId(@Param("characterId") String characterId);

    BlockedCharacterEntity findByCharacterIdAndBlockedCharacterId(String characterId, String blockedCharacterId);

    @Query("SELECT b FROM BlockedCharacterEntity b WHERE " +
        "(b.characterId = :characterId AND b.blockedCharacterId = :blockedCharacterId)" +
        " OR " +
        "(b.characterId = :blockedCharacterId AND b.blockedCharacterId = :characterId)")
    List<BlockedCharacterEntity> findByCharacterIdOrBlockedCharacterId(@Param("characterId") String characterId, @Param("blockedCharacterId") String blockedCharacterId);
}

它具有一个DAO类,该类提供对存储库的访问并处理实体和dto之间的转换:

package org.github.saphyra.skyxplore.community.blockedcharacter.repository;

import java.util.List;

import org.springframework.stereotype.Component;

import com.github.saphyra.converter.Converter;
import com.github.saphyra.dao.AbstractDao;
import lombok.extern.slf4j.Slf4j;
import org.github.saphyra.skyxplore.community.blockedcharacter.domain.BlockedCharacter;

@Component
@Slf4j
public class BlockedCharacterDao extends AbstractDao<BlockedCharacterEntity, BlockedCharacter, Long, BlockedCharacterRepository> {
    public BlockedCharacterDao(Converter<BlockedCharacterEntity, BlockedCharacter> converter, BlockedCharacterRepository repository) {
        super(converter, repository);
    }

    public void deleteByCharacterId(String characterId) {
        repository.deleteByCharacterId(characterId);
    }

    public BlockedCharacter findByCharacterIdAndBlockedCharacterId(String characterId, String blockedCharacterId) {
        return converter.convertEntity(repository.findByCharacterIdAndBlockedCharacterId(characterId, blockedCharacterId));
    }

    public List<BlockedCharacter> findByCharacterIdOrBlockedCharacterId(String characterId, String blockedCharacterId) {
        return converter.convertEntity(repository.findByCharacterIdOrBlockedCharacterId(characterId, blockedCharacterId));
    }

    public List<BlockedCharacter> getBlockedCharactersOf(String characterId) {
        return converter.convertEntity(repository.findByCharacterId(characterId));
    }
}

我可以保存新实体,可以查询实体列表,但是当我调用repository.findByCharacterIdAndBlockedCharacterId(characterId, blockedCharacterId)时,会收到一个IllegalAccessError:

Caused by: java.lang.IllegalAccessError: tried to access class org.github.saphyra.skyxplore.community.blockedcharacter.repository.BlockedCharacterEntity from class com.sun.proxy.$Proxy105
    at com.sun.proxy.$Proxy105.findByCharacterIdAndBlockedCharacterId(Unknown Source)
    at org.github.saphyra.skyxplore.community.blockedcharacter.repository.BlockedCharacterDao.findByCharacterIdAndBlockedCharacterId(BlockedCharacterDao.java:24)
    at org.github.saphyra.skyxplore.community.blockedcharacter.BlockedCharacterQueryService.findByCharacterIdAndBlockedCharacterId(BlockedCharacterQueryService.java:19)
    at org.github.saphyra.skyxplore.community.blockedcharacter.BlockCharacterService.allowBlockedCharacter(BlockCharacterService.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
    at org.github.saphyra.skyxplore.community.blockedcharacter.BlockCharacterService$$EnhancerBySpringCGLIB$$bf4b60be.allowBlockedCharacter(<generated>)
    at org.github.saphyra.skyxplore.community.blockedcharacter.BlockedCharacterFacade.allowBlockedCharacter(BlockedCharacterFacade.java:19)
    at org.github.saphyra.skyxplore.community.blockedcharacter.BlockedCharacterController.allowBlockedCharacter(BlockedCharacterController.java:39)

当我将实体的可见性设置为public时,不会引发异常。 我还有其他10个私有包实体类,它们都可以正常工作。 其他实体没有Long类型自动生成的主键的唯一区别是它们使用String PK-s。

为什么会出现此异常?

0 个答案:

没有答案