o.s.d.jpa.repository.query.NamedQuery:在多模块项目中找不到命名查询...

时间:2020-03-18 06:07:11

标签: java hibernate spring-boot spring-data-jpa

我有多模块项目(* .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);
    }
}

模型

  • IdMainForEntities
@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 + '\'' +
                '}';
    }
}
  • MessageLog

@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;
    }

....


}

  • MessagesLogReadRepository
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 o.s.d.j.r.query.JpaQueryFactory
:查找查询方法findByMsgOwnerId 18-03-2020 08:49:39.547调试8660 o.s.d.jpa.repository.query.NamedQuery: 查找命名查询MessageLog.findByMsgOwnerId 18-03-2020 08:49:39.547调试8660 o.h.e.t.internal.TransactionImpl: 在创建TransactionImpl时, JpaCompliance#isJpaTransactionComplianceEnabled ==否18-03-2020 08:49:39.547调试8660 o.s.d.jpa.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

但是,它不能解决问题。

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

有人对此有任何想法吗?

为什么?

1 个答案:

答案 0 :(得分:0)

解决方案

我们正在使用的字段的数据类型为 CHAR(32字节)。我们将此类型更改为 VARCHAR(32字节)到Oracle的表数据库中。

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

旧版Spring上的项目的工作方式如下...( CHAR(32字节)

我不明白为什么。