Spring仓库查询一次又一次返回相同的值。

huangapple 未分类评论48阅读模式
英文:

Spring repository query returns the same value over and over

问题

我在使用Spring时遇到了一个问题,当我输入一个本地查询(native query)时,返回的部分数据是正确的,但其他字段的值一直相同。

该项目有公司对象(Company objects),每个公司对象都有一个表示针对该公司发布的标题的情感对象(Sentiment objects)列表,包括标题发布的日期、当天的公司股价,以及神经网络认为股价在接下来的24小时内可能变动的幅度(但现在这不是很重要)。

问题是,当我使用Spring从数据库中提取情感数据时,所有情感对象的id都是0,股价是0,日期都是相同的控制台输出显示所有id都是0,所有股价都是0,所有日期都是2020-02-27,但标题如预期更改。以下是我使用的查询:

@Query(value="SELECT sentiment.* FROM sentiment, company, company_sentiment WHERE (company.id=company_sentiment.Company_id) AND (sentiment.id=company_sentiment.sentimentData_id) AND (company.id= :keyword)",
       nativeQuery=true)
public List<Sentiment> getSentiment(@Param("keyword") String keyword);

与此相反,数据库中的数据是正确的。
日期和预测在几天内保持一致是可以的,因为数据重叠,但始终为零意味着在读入Web应用程序时出现了问题。
由于这个问题,我为该项目制作的网站看起来毫无意义。

您是否遇到过类似的问题?

英文:

I'm having an issue with Spring when I feed in a native query, some of the data it returns is correct, but other fields are returned as the same value over and over.

The project has Company objects, and each company object has a list of Sentiment objects, representing headlines posted about the company, the date that the headline was posted, the companies stock price that day, and the value that a neural network believes the stock price will move by in the next 24 hours (but that's not that important right now.

The problem is that when I use spring to pull the sentiment data from the database, all sentiment objects are returned with id=0 stockprice=0 and all with the same date. Console output shows that all id=0, all stock prices=0, all dates = 2020-02-27, but headlines are changing as expected. Here's the query I use:

@Query(value=&quot; SELECT sentiment.* FROM sentiment, company, company_sentiment WHERE(company.id=company_sentiment.Company_id) AND (sentiment.id=company_sentiment.sentimentData_id) AND (company.id=&#39;%&#39; || :keyword || &#39;%&#39;)&quot;,
			nativeQuery=true)
	public List&lt;Sentiment&gt; getSentiment(@Param(&quot;keyword&quot;) String keyword);

Contrary to this, the data is fine inside the database.
The dates and prediction being the same across a few days are fine, as data overlaps but the fact that it's always zero means somethings wrong with it being read into the web app
As a result of this,the website I made for the project looks pointless..

Have you ever encountered a problem like this?

答案1

得分: 0

尝试这样做:在方法的参数关键字中,不要使用@Param("keyword"),而是像这样:

public List<Sentiment> getSentiment(String keyword);

在查询中,不要使用:keyword,而是使用?1

希望这能有所帮助。

英文:

try this: in the argument keyword in the method don't use @Param(&quot;keyword&quot;)

but like this

public List&lt;Sentiment&gt; getSentiment(String keyword);

and in the query instead of :keyword use ?1

I hope it helps

huangapple
  • 本文由 发表于 2020年5月4日 23:54:36
  • 转载请务必保留本文链接:https://java.coder-hub.com/61596348.html
匿名

发表评论

匿名网友

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

确定