我有两个需要连接在一起的表。
下面是我的模型课
@Data
@Entity
@Table(name="storetypecode")
public class StoreTypeCode implements IModel {
@Id
@EqualsAndHashCode.Exclude
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
@Column(name="isenabled", nullable = false)
private boolean isEnabled = false;
@Column(name="displaysort", nullable = false)
private Integer displaySort = 999;
@Setter(AccessLevel.NONE)
@OneToMany(fetch = FetchType.EAGER, mappedBy = "storeTypeCode")
private Set<StoreTypeCodeTranslation> storeTypeCodeTranslationList = new HashSet<>();
public StoreTypeCode(){
// Do nothing
}
}
@Data
@Entity
@Table(name="storetypecodetranslation")
public class StoreTypeCodeTranslation implements IModel {
@Id
@EqualsAndHashCode.Exclude
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID id;
@Column(name = "type1")
private String type1;
@Column(name = "type2")
private String type2;
@Column(name = "lang", nullable = false)
private String lang="en";
@Column(name="isdefault", nullable = false)
private boolean isDefault=false;
@ManyToOne
@JsonIgnore
@ToString.Exclude
@EqualsAndHashCode.Exclude
@JoinColumn(name = "storetypecode_id")
private StoreTypeCode storeTypeCode;
public StoreTypeCodeTranslation(){
// Do nothing
}
}
StoreTypeCodeTranslation表充当StoreTypeCode的转换。 在SQL中,我将使用以下命令获取特定语言的StoreTypeCode。
SELECT STC.id, STC.displaysort, STT.type1, STT.type2
FROM storetypecode AS STC
LEFT JOIN storetypecodetranslation AS STT on STC.id = STT.storetypecode_id
WHERE STT.lang = 'en'
ORDER BY STC.displaysort ASC
如何使用JPARepository获得上面SQL代码中编写的结果?另外,如果没有翻译成语言的StoreTypeCode,它应该返回带有'isDefault = true'的结果。
我还是JPARepository的新手,并试图从其库中获得最大的收益。如果有人可以指导我从哪里开始,将不胜感激。
谢谢
更新------------------------ 我有以下存储库设置。
public interface StoreTypeCodeRepository extends JpaRepository<StoreTypeCode, UUID> {
@Query(
value = "SELECT STC.id, STC.displaysort, STT.type1, STT.type2\n"
+ "FROM storetypecode AS STC\n"
+ "LEFT JOIN storetypecodetranslation AS STT on STC.id = STT.storetypecode_id\n"
+ "WHERE STT.lang = ?1\n"
+ "ORDER BY STC.displaysort ASC", nativeQuery = true)
List<StoreTypeCodeResult> findByLanguage(String language);
}
然后我将StoreTypeCodeResult创建为DTO类,如下所示:
public class StoreTypeCodeResult {
@Getter
@Setter
@Type(type="pg-uuid")
private UUID id;
@Getter
@Setter
private Integer displaySort = 999;
@Getter
@Setter
private String type1;
@Getter
@Setter
private String type2;
public StoreTypeCodeResult(){
}
}
运行代码时,出现错误,提示JDBC类型没有No Dialect映射:1111;嵌套的异常是org.hibernate.MappingException:没有JDBC类型的方言映射:1111
我在这里做错什么了吗?