为什么实体无法映射列表中的列?

huangapple go评论111阅读模式
英文:

why the entity can't map the columns table?

问题

  1. 我有一个名为"countries"MySQL表,结构如下:

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

| .. | .. | ... | ...| ....| ... | ... | ... |

  1. 有一个实体叫做"customCountry"
  2. ```java
  3. @Entity
  4. @Table(name="countries")
  5. public class customCountry {
  6. @Id
  7. @Column(name = "id", table ="countries")
  8. private Long id;
  9. @Column(name = "code", table ="countries")
  10. private Long code;
  11. @Column(name = "language", table ="countries")
  12. private String language;
  13. // 构造函数 + getter/setter
  14. }

我有这个Repository:

  1. @Repository
  2. public interface customCountryRepository extends JpaRepository<customCountry, Long>{
  3. List<customCountry> findAll();
  4. }

以及这个Service类:

  1. @Service
  2. @ComponentScan(basePackages="repository")
  3. public class countriesService {
  4. @Autowired
  5. customCountryRepository customcountry_repo;
  6. public List<customCountry> loadcustomcountries() {
  7. List<customCountry> validcustomcountries = customcountry_repo.findAll();
  8. System.out.println("size: " + validcustomcountries.size());
  9. return validcustomcountries;
  10. }
  11. }

问题是,当我在控制器中调用这个Service方法时,我发现大小为0,但我不知道为什么,我认为我已经很好地管理了属性和列之间的映射。谢谢

  1. <details>
  2. <summary>英文:</summary>
  3. i have a mysql table &quot;countries&quot; as follow

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

| .. | .. | ... | ...| ....| ... | ... | ... |

  1. this entity &quot;customCountry&quot;:

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

  1. @Column(name = &quot;code&quot;,table =&quot;countries&quot;)
  2. private Long code;
  3. @Column(name = &quot;language&quot;,table =&quot;countries&quot;)
  4. private String language;

//constructors + getters/setters
}

  1. i have this repository :

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

  1. List&lt;customCountry&gt; findAll();

}

  1. and this service class :

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

  1. @Autowired
  2. customCountryRepository customcountry_repo;
  3. public List&lt;customCountry&gt; loadcustomcountries()
  4. {
  5. List&lt;customCountry&gt; validcustomcountries = customcountry_repo.findAll();
  6. System.out.println(&quot;size: &quot;+validcustomcountries.size());
  7. return validcustomcountries;
  8. }

}

  1. the problem is that when i called this service method in my controller , i find out that the size = 0 , but i don&#39;t see why , i think that i well managed the mapping between attributes and columns though .
  2. thank you
  3. </details>
  4. # 答案1
  5. **得分**: 0
  6. 你可以从仓库类中移除 **`List&lt;customCountry&gt; findAll();`**。
  7. 因为你已经扩展了 **`JpaRepository&lt;customCountry,Long&gt;`**,它已经有了这个方法。
  8. 尽管你在仓库中提供了这个方法,但没有实现它。
  9. 这可能会导致问题。
  10. 试试移除它看看。
  11. <details>
  12. <summary>英文:</summary>
  13. You can remove **`List&lt;customCountry&gt; findAll();`** from repository class.
  14. As you had already extended **`JpaRepository&lt;customCountry,Long&gt;`**, it&#39;s have this method.
  15. As you are providing that method in repository but not implementation of it.
  16. That is causing an issue.
  17. Just try by removing it.
  18. </details>

huangapple
  • 本文由 发表于 2020年8月24日 01:07:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63549813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定