英文:
why the entity can't map the columns table?
问题
我有一个名为"countries"的MySQL表,结构如下:
| id | name | name2 | code | area | currency | language | population |
| .. | .. | ... | ...| ....| ... | ... | ... |
有一个实体叫做"customCountry":
```java
@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;
// 构造函数 + getter/setter
}
我有这个Repository:
@Repository
public interface customCountryRepository extends JpaRepository<customCountry, Long>{
List<customCountry> findAll();
}
以及这个Service类:
@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;
}
}
问题是,当我在控制器中调用这个Service方法时,我发现大小为0,但我不知道为什么,我认为我已经很好地管理了属性和列之间的映射。谢谢
<details>
<summary>英文:</summary>
i have a mysql table "countries" as follow
| id | name | name2 | code | area | currency | language | population |
| .. | .. | ... | ...| ....| ... | ... | ... |
this entity "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
}
i have this repository :
@Repository
public interface customCountryRepository extends JpaRepository<customCountry,Long>{
List<customCountry> findAll();
}
and this service class :
@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;
}
}
the problem is that when i called this service method in my controller , i find out that the size = 0 , but i don't see why , i think that i well managed the mapping between attributes and columns though .
thank you
</details>
# 答案1
**得分**: 0
你可以从仓库类中移除 **`List<customCountry> findAll();`**。
因为你已经扩展了 **`JpaRepository<customCountry,Long>`**,它已经有了这个方法。
尽管你在仓库中提供了这个方法,但没有实现它。
这可能会导致问题。
试试移除它看看。
<details>
<summary>英文:</summary>
You can remove **`List<customCountry> findAll();`** from repository class.
As you had already extended **`JpaRepository<customCountry,Long>`**, it's have this method.
As you are providing that method in repository but not implementation of it.
That is causing an issue.
Just try by removing it.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论