【问题标题】:why the entity can't map the columns table?为什么实体不能映射列表?
【发布时间】:2020-12-12 10:20:54
【问题描述】:

我有一个mysql表“国家”如下

| id | name | name2 | code | area | currency | language | population |
----------------------------------------------------------------------
| .. | ..   |   ... |   ...|  ....|  ...     |   ...    |   ...      |

这个实体“customCountry”:

@Entity
@Table(name="countries")
public class customCountry {
    @Id
    @Column(name = "id",table ="countries")
     private Long id;

    @Column(name = "code",table ="countries")
     private Long code;

    @Column(name = "language",table ="countries")
     private String language;

   //constructors + getters/setters
}

我有这个存储库:

@Repository
public interface customCountryRepository extends JpaRepository<customCountry,Long>{

    
    List<customCountry> findAll();
}

还有这个服务类:

@Service
@ComponentScan(basePackages="repository")
public class countriesService {

    @Autowired
    customCountryRepository customcountry_repo;
    
    public List<customCountry> loadcustomcountries()
    {
        List<customCountry> validcustomcountries = customcountry_repo.findAll();
        System.out.println("size: "+validcustomcountries.size());
        return validcustomcountries;
    }
}

问题是,当我在控制器中调用此服务方法时,我发现 size = 0,但我不明白为什么,我认为我很好地管理了属性和列之间的映射。 谢谢

【问题讨论】:

  • 您的表中有数据行吗?还是空的..?
  • 是的!我在表中有数据行

标签: java spring jpa spring-data-jpa entity


【解决方案1】:

您可以从存储库类中删除 List&lt;customCountry&gt; findAll();

因为你已经扩展了JpaRepository&lt;customCountry,Long&gt;,所以有这个方法。

当您在存储库中提供该方法而不是实现它时。 这导致了一个问题。

删除它试试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 2019-03-31
    • 2013-10-31
    • 1970-01-01
    • 2020-04-02
    • 2016-02-21
    相关资源
    最近更新 更多