在我的项目中使用spring数据jpa
package com.mf.acrs.model;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Data
@Entity(name= "mv_garage_asset_mapping")
public class GarageAssetMapping implements Serializable {
/**
*
*/
private static final long serialVersionUID = 2535545189473989744L;
@Id
@Column(name="GARAGE_CODE")
private String garageCode;
@Column(name="GARAGE_NAME")
private String garageName;
@Column(name="GARAGE_ADDRESS")
private String garageAddress;
@Column(name="GARAGE_BRANCH")
private String garageBranch;
@Column(name="CONTRACT_NUMBER")
private String contractNumber;
}
这是我的实体对象
package com.mf.acrs.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import com.mf.acrs.model.GarageAssetMapping;
public interface GarageAssetMappingRepository extends JpaRepository<GarageAssetMapping, String> {
// @Query(name="select u.CONTRACT_NUMBER from mv_garage_asset_mapping u where u.GARAGE_CODE = ?1", nativeQuery = true) //**QUERY 1**
@Query("select u.contractNumber from mv_garage_asset_mapping u where u.garageCode = ?1") // **QUERY 2**
List<String> findByGarageCode(String garageCode);
}
这是我的存储库界面
当我在应用程序中使用QUERY 1时,spring数据jpa触发的查询为
休眠状态:选择garageasse0_.garage_code作为garage_code1_2_,garageasse0_.contract_number作为contract_number2_2_,garageasse0_.garage_address作为garage_address3_2_,garageasse0_.garage_branch作为garage_branch4_2_,garageasse0_garage_0从Garage_asses_asset_asset_vage_asset_asset_asset_asset_asses。 >
但是当我使用QUERY 2时,触发的查询是
休眠:从mv_garage_asset_mapping garageasse0_中选择garageasse0_.contract_number作为col_0_0_,其中garageasse0_.garage_code =?
QUERY 2给了我想要的结果。 但是我的问题是为什么在第一种情况下spring data jpa会触发错误的查询。 尽管我已经在查询中明确地写了我只想获取一个字段的事实,但是在QUERY 1中,hibernate尝试提取所有数据字段。
我在这种情况下会犯什么错误?
在控制器中定义的调用该方法的方法如下:
@PostMapping("/searchAssetsAjax")
@ResponseBody
public String searchAssetsAjax(@RequestBody SearchAssetData searchAssetData) throws IOException{
System.out.println("iam in the searchAssetsAjax "+searchAssetData);
System.out.println("iam in the searchAssetsAjax "+searchAssetData.toString());
// System.out.println("throwing exceptions" ); throw new IOException();
System.out.println("hitting the db "+searchAssetData.getGarageCode());
// List<String> contractNums = garageAssetMapRepository.findContractNumberByGarageCode(searchAssetData.getGarageCode());
List<String> contractNums = garageAssetMapRepository.findByGarageCode(searchAssetData.getGarageCode());
System.out.println("@@@@@@@@@@@@contract num size is "+contractNums.size());
for(String contract: contractNums) {
System.out.println("contract nums are "+contract);
}
return "success";
}