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

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

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 &quot;countries&quot; as follow 

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

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

this entity &quot;customCountry&quot;:

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

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

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

//constructors + getters/setters
}

i have this repository :

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

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

}


and this service class :

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

@Autowired
customCountryRepository customcountry_repo;

public List&lt;customCountry&gt; loadcustomcountries()
{
	List&lt;customCountry&gt; validcustomcountries = customcountry_repo.findAll();
	System.out.println(&quot;size: &quot;+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&#39;t see why , i think that i well managed the mapping between attributes and columns though .
thank you

</details>


# 答案1
**得分**: 0

你可以从仓库类中移除 **`List&lt;customCountry&gt; findAll();`**。

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

尽管你在仓库中提供了这个方法,但没有实现它。
这可能会导致问题。

试试移除它看看。

<details>
<summary>英文:</summary>

You can remove  **`List&lt;customCountry&gt; findAll();`** from repository class.

As you had already extended **`JpaRepository&lt;customCountry,Long&gt;`**, it&#39;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>



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:

确定