我有两张桌子" table1"和" table2"。对于table1中的每一行,table2中可以有多行。为了获取数据,我创建了以下类
SharedPreferences prefs = this.getSharedPreferences("Demo", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
prefs.getString("yourKey","");
Log.d("URL",""+ prefs.getString("yourKey",""));
edit.commit();
和
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "table1", schema = "dbo")
public class Table1 extends BaseDomain{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer vendorId;
private String name;
private Integer companyId;
private String details;
@OneToMany(mappedBy = "table1",cascade = CascadeType.ALL)
@NotFound(action = NotFoundAction.IGNORE)
private Set<Table2> table2;
// getter and setter for above
}
然后我创建了像这样的存储库
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "table2", schema = "dbo")
public class Table2 extends BaseDomain{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private Integer cityId;
private Integer companyId;
@OneToOne
@JoinColumn(name = "vendorId")
@JsonIgnore
private Table1 table1;
// getter and setter for above
}
这将根据companyId给出table1中的行列表,并给出表2中具有相应vendorId的行列表。
直到现在我的实施工作正常。
回复是
@Transactional
public interface Table1Repository extends JpaRepository<Table1, Integer> {
Page<Table1> findByCompanyId(Integer companyId,Pageable pageable);
}
目前我正在将companyId作为输入并给出上述回复。现在我想把cityId作为输入。因此table2输出应仅包含给定cityId的详细信息。
所以任何人都可以帮助我如何实现它?我不想为table1和table2创建另一个模型。
答案 0 :(得分:1)
Page<Table1> findByCompanyIdAndTable2_CityId(Integer companyId, Integer cityId, Pageable pageable);
此方法将负责cityId的提取。下划线(_)指嵌套字段。但要实现这一点,您需要急切地获取Table2。
@OneToMany(mappedBy = "table1",cascade = CascadeType.ALL, fetch=FetchType.EAGER)