首页 > 解决方案 > osdjpa.repository.query.NamedQuery:在多模块项目中没有找到命名查询...

问题描述

我有多模块项目(* .war)。

它是应用程序的入口点。

@SpringBootConfiguration
@SpringBootApplication
@EnableJpaRepositories(basePackages = {"....dao.repository"})
@EntityScan(basePackages = {"....dao.model"})
@ComponentScan(basePackages = {"..."})
public class ApsTtsApplication
        extends SpringBootServletInitializer
        implements WebApplicationInitializer {

    private static final Logger LOGGER  = LoggerFactory.getLogger( ApsTtsApplication.class );

    public static void main(String[] args) {

        LOGGER.info("Start an application...");

        SpringApplication.run(ApsTtsApplication.class, args);
    }


    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {

        LOGGER.info("There is building the web application!");

        return builder.sources(ApsTtsApplication.class);
    }
}

模型

@MappedSuperclass
public abstract class IdMainForEntities {

    @Id
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    @GeneratedValue(generator="system-uuid")
    @Column(name = "id", nullable = false)
    private String ID;

    public IdMainForEntities() {
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        IdMainForEntities that = (IdMainForEntities) o;
        return Objects.equals(ID, that.ID);
    }

    @Override
    public int hashCode() {
        return Objects.hash(ID);
    }

    @Override
    public String toString() {
        return "IdMainEntity{" +
                "ID='" + ID + '\'' +
                '}';
    }
}

@Entity
@Table(name = "MESSAGESLOG")
public class MessageLog extends IdMainForEntities {
...

    @Column(name = "MSG_OWNER_ID")
    @Size(message = "MSG_OWNER_ID{MessagesLog.size}", max = 32)
    private String msgOwnerId;

  public MessageLog() {
    }

  public String getMsgOwnerId() {
        return msgOwnerId;
    }

    public void setMsgOwnerId(String msgOwnerId) {
        this.msgOwnerId = msgOwnerId;
    }

....


}

public interface MessagesLogReadRepository extends CrudRepository <MessageLog, String> {

    Optional <MessageLog> findByMsgOwnerId(String msgOwnerId);

    MessageLog findMessageLogByMsgOwnerId(String msgOwnerId);

    Optional <MessageLog> findByDocument(String documentId);

}

只能从此存储库调用标准方法:findById ()等。

但未找到命名查询。

18-03-2020 08:49:39.531 调试 8660 osdjrquery.JpaQueryFactory
:查找方法 findByMsgOwnerId 的查询 18-03-2020 08:49:39.547 调试 8660 osdjpa.repository.query.NamedQuery:查找命名查询 MessageLog.findByMsgOwnerId 18 -03-2020 08:49:39.547 调试 8660 ohetinternal.TransactionImpl:在创建 TransactionImpl 时,JpaCompliance#isJpaTransactionComplianceEnabled == false 18-03-2020 08:49:39.547 调试 8660 osdjpa.repository.query.NamedQuery:未找到命名查询MessageLog.findByMsgOwnerId

@Override
    public MessageLogDto getByMsgOwnerId(String msgOwnerId) {

 if(msgOwnerId == null) throw  new MessagesLogException(paramNotNull);

/*It's null*/
        Optional<MessageLog> byMsgOwnerId = this.messagesLogReadRepository.findByMsgOwnerId("8a00844170d829040170d82c670b00");
        MessageLog messageLog = byMsgOwnerId.orElse(new MessageLog());
     /*It's OK*/
        Optional<MessageLog> byId = this.messagesLogReadRepository.findById("8a00844170d829040170d82c670b0003");

        return transformEntityToDto(messageLog);

       }

更新

public interface MessagesLogReadRepository extends JpaRepository<MessageLog, String> {


    @Query(value = "SELECT * from messageslog where MSG_OWNER_ID = ?1", nativeQuery = true )
    Optional <MessageLog> findRowByMsgOwnerId(String msgOwnerId);

...

...扩展 JpaRepository

但是,它并不能解决问题。

我没有任何错误。我只得到空值,但请求的行在表中,这是肯定的。

有人对此有任何想法吗?

为什么 ?

标签: javahibernatespring-bootspring-data-jpa

解决方案


解决方案

我们正在使用的字段具有数据类型CHAR(32 byte)。我们把这个类型改成VARCHAR(32 byte)到Oracle的表数据库中。

现在,Spring 可以很好地构建命名查询。

旧版本 Spring 上的项目是这样工作的 ...( CHAR(32 byte) )

我不明白为什么会这样。


推荐阅读