Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

左外联查询找不到字段 #36

Open
ShisiJu opened this issue Jun 3, 2019 · 3 comments
Open

左外联查询找不到字段 #36

ShisiJu opened this issue Jun 3, 2019 · 3 comments

Comments

@ShisiJu
Copy link

ShisiJu commented Jun 3, 2019

eq(tutorStudno != null, "course.tutor.studno", tutorStudno)

当执行上面eq进行查询时,会出现下面的错误

java.lang.IllegalArgumentException:
Unable to locate Attribute with the the given name [tutor.studno]
on this ManagedType [com.jss.app.model.base.RowBase]

我确定字段名没有写错,而且使用JPA的简单查询是能够查出来的

JPA简单查询的代码

// 下面的代码可以执行,并正确返回结果
List<StudentCourse> findByCourse_Tutor_Studno(String tutorStudno);

具体的实现细节

// 省略一些属性的读取
String studentStudno = jsonObject.getString("studentStudno");
String tutorStudno = jsonObject.getString("tutorStudno");
Specification<StudentCourse> specification = Specifications.<StudentCourse>and()
				.eq(studentId != null, "student.id", studentId)
				.like(!StringUtils.isEmpty(name), "course.name", "%" + name + "%")
				.eq(academicYear != null, "course.academicYear", academicYear)
                                .eq(term != null, "course.term", term)
				.eq(courseTyoe != null, "course.courseTyoe", courseTyoe)
				.eq(!StringUtils.isEmpty(studentStudno), "student.studno", studentStudno)
				.eq(!StringUtils.isEmpty(tutorStudno), "course.tutor.studno", tutorStudno).build();

但是,student.studnocourse.courseTyoe 等查询没有问题;

下面是实体类

StudentCourse

@Entity
@Table(name = "T_STUDENT_COURSE")
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = false)
public class 	StudentCourse  extends RowBase {
	private static final long serialVersionUID = -164015745985836181L;
	@ManyToOne(cascade = CascadeType.REMOVE)
	private Student student;
	@ManyToOne(cascade = CascadeType.REMOVE)
	private Course course;
	@ColumnDefault(value = "0")
	private Integer status;
	private Integer score;
}

Course

@Proxy(lazy = false)
@Entity
@Table(name = "T_COURSE")
@Builder
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = false)
@Accessors(chain = true)
public class Course extends CommonDictionary implements Serializable {
	private static final long serialVersionUID = -840690077388663221L;
	@ManyToOne
	private Tutor tutor;
	@ManyToMany
	@JoinTable(name = "T_COURSE_GROUP")
	private List<Group> group;
	private Integer academicYear;
	@Enumerated
	private Term term;
	@Enumerated
	@ColumnDefault(value = "0")
	private CourseTyoe courseType;
	@ColumnDefault(value = "0")
	private Integer amount;
	@ColumnDefault(value = "0")
	private Integer currentNum;

}

Tutor

@Entity
@Table(name = "T_TUTOR")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class Tutor extends RowBase implements Serializable {

	private static final long serialVersionUID = 281326835164141801L;
	private String name;

	@Enumerated
	private Sex sex;
	private String studno;
	private String title;
	@ManyToOne
	private Institute institute;
}

RowBase

@MappedSuperclass
@Data
@NoArgsConstructor
@AllArgsConstructor
@DynamicInsert(true)
@DynamicUpdate
@Accessors(chain = true)
public class RowBase implements Serializable {

	private static final long serialVersionUID = -8386266958781724931L;
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	@Column(updatable = false)
	@CreationTimestamp
	private Date createTime;
	@UpdateTimestamp
	private Date updateTime;
}
@ShisiJu
Copy link
Author

ShisiJu commented Jun 3, 2019

image
附一张,调式时的截图,希望有帮助

@wenhao
Copy link
Owner

wenhao commented Jun 5, 2019

好的,稍后就看看

@ShisiJu
Copy link
Author

ShisiJu commented Jun 9, 2019

感谢你的回复

我后来经过实验,
我将tutor用简单查询查出来后,在进行查询时,可以得到结果。
但这样有些不方便。

希望能有所帮助

Tutor tutor = tutorRepository.findByStudno(tutorStudno);

Specification<StudentCourse> specification = Specifications.<StudentCourse>and()
		.eq(!StringUtils.isEmpty(tutorStudno), "course.tutor", tutor).build();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants