Java 无法使用命名查询在单例EJB中找到私有实体
我有一个单例EJB,它在开始时初始化两个实体:Java 无法使用命名查询在单例EJB中找到私有实体,java,oracle,jpa,ejb,Java,Oracle,Jpa,Ejb,我有一个单例EJB,它在开始时初始化两个实体: @Startup @Singleton @Lock(LockType.READ) public class PetShopSingleton { @Inject private AnimalRepository animalRepository; private Animal cat; private Animal dog; @PostConstruct public void init() { cat = AnimalRepos
@Startup
@Singleton
@Lock(LockType.READ)
public class PetShopSingleton {
@Inject
private AnimalRepository animalRepository;
private Animal cat;
private Animal dog;
@PostConstruct
public void init() {
cat = AnimalRepository.findByType(AnimalType.CAT);
dog = AnimalRepository.findByType(AnimalType.DOG);
}
}
实体是从运行命名查询的服务初始化的:
@Stateless
public class AnimalRepository extends BaseRepository {
public Animal findByType(AnimalType type) {
Map<String, Object> params = new HashMap<>();
params.put("type", type);
return findOneByNamedQuery("Animal.findByType", params,
Animal.class);
}
}
最后是持久实体:
@Entity
@Table(name = "ANIMALS")
@NamedQueries({
@NamedQuery(
name = "Animal.findByType",
query = "select a from Animal a where a.type = :type"
)
})
public class Animal {
@Enumerated(EnumType.STRING)
private AnimalType type;
public AnimalType getType() {
return type;
}
public void setTyp(AnimalType type) {
this.type = type;
}
}
一个实体(cat)是较旧的实体,已在数据库中注册,较新的实体(dog)最近已注册。这里的问题是,在init()函数中调用的用于查找具有命名查询的两个实体的服务仅查找cat。虽然数据库中有一个条目的类型列等于dog,但dog返回null
有谁能帮我一下,或者告诉我发生这种情况的可能原因吗
数据库是一个Oracle数据库。好的,我解决了这个问题,表中有一列包含动物头像的文件路径,以C:/path/to/avatar开头,我将它改为/path/to/avatar,它成功了。仍然不知道为什么会发生这种情况,但现在没事了:)
public enum AnimalType {
CAT,
DOG;
}
@Entity
@Table(name = "ANIMALS")
@NamedQueries({
@NamedQuery(
name = "Animal.findByType",
query = "select a from Animal a where a.type = :type"
)
})
public class Animal {
@Enumerated(EnumType.STRING)
private AnimalType type;
public AnimalType getType() {
return type;
}
public void setTyp(AnimalType type) {
this.type = type;
}
}