我的代码结构如下所示。
文章:
@Entity
public class NewsArticle{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
[Other class properties such as title, publisher, publishedDate, etc.]
@OneToMany(mappedBy = "article")
private Set<UserReadNewsArticle> userReadNewsArticles = new HashSet<>();
[Getters and Setters]
}
用户阅读的文章:
@Entity
public class UserReadNewsArticle {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private Long readAccountId;
private Long readArticleId;
@JsonIgnore
@ManyToOne
private Account account;
@JsonIgnore
@ManyToOne
private NewsArticle article;
[Getters and Setters]
}
帐户:
@Entity
public class Account {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
[Other class properties]
@OneToMany(mappedBy = "account")
private Set<UserReadNewsArticle> userReadNewsArticles = new HashSet<>();
[Getters and Setters]
}
我希望在我的NewsArticleRepository中有一个查询方法来获取用户的所有阅读新闻文章。
public interface NewsArticleRepository extends PagingAndSortingRepository<NewsArticle, Long>{
Collection<NewsArticle> findByUserReadNewsArticlesReadAccountId(Long readAccountId);
}
这种方法效果很好。但是如何编写Spring Data JPA查询/方法来为用户获取&#34;未读新闻文章&#34;。我试过的是以下内容。
Collection<NewsArticle> findByUserReadNewsArticlesReadAccountIdNot(Long readAccountId);
这个确实返回了其他用户已阅读的文章列表。但我的要求是获取所有未读的新闻文章。我经历过Spring Data JPA Documentation但未能提出更容易的灵魂。我怎样才能克服这个问题?或者我做错了什么?
答案 0 :(得分:1)
您可以通过使用带有子查询的JPQL查询来实现结果:
def loss(self,X,y,reg = 0.0):
#forward prop
W1, b1 = self.params['W1'], self.params['b1']
W2, b2 = self.params['W2'], self.params['b2']
W3, b3 = self.params['W3'], self.params['b3']
N,D = X.shape
H1out = np.maximum(0,X.dot(W1)+b1) #H1out (N,H1)
H2out = np.maximum(0,H1out.dot(W2)+b2) #H2out (N,H2)
scores = None
scores = H2out.dot(W3)+b3
scores_shift = scores-np.max(scores,axis = 1).reshape(-1,1)
softmaxout = np.exp(scores_shift)/np.sum(np.exp(scores_shift),axis=1).reshape(-1,1)
loss_main = None
loss = None
loss_main = -np.sum(np.log(softmaxout[range(N),list(y)]))
loss = loss_main/N + reg*np.sum(W1*W1)*np.sum(
W2*W2)+np.sum(W3*W3)
#backward prop
dscores = softmaxout.copy() #dscores (N,C)
dscores[range(N),list(y)] -= 1
dscores /= N
dW3 = H2out.T.dot(dscores)
db3 = np.sum(dscores,axis = 0)
dh2 = dscores.dot(W3.T) #dh2 (N,H2)
dh_Relu2 = (H2out > 0) * dh2 #dh_ReLu2 (N,H2)
dW2 = H1out.T.dot(dh_Relu2)
db2 = np.sum(dh_Relu2,axis = 0)
dh1 = dh_Relu2.dot(W2.T) #dh1 (N,H1)
dh_Relu1 = (H1out>0) * dh1
dW1 = X.T.dot(dh_Relu1)
db1 = np.sum(dh_Relu1,axis = 0)
grad = {}
grad['W1'] = dW1
grad['b1'] = db1
grad['W2'] = dW2
grad['b2'] = db2
grad['W3'] = dW3
grad['b3'] = db3
return loss,grad
首先从当前用户那里获取阅读清晰度,然后将其从整篇文章列表中排除。
我不认为spring数据能够让你变得一样,因为子查询是绝对需要的。如果我错了,有人可以纠正我。