I have designed the entities in my application to follow the Hibernate's inheritance strategy Inheritance.JOINED
.
The base abstract class is Activity
and the concrete derived classes are Exercise
and Contest
:
(currently, Exercise
and Contest
are almost identical)
Activity.java
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "activity")
@Data
@NoArgsConstructor
@AllArgsConstructor
public abstract class Activity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "activity_id")
private Integer id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "details")
private String details;
@ManyToOne
@JoinColumn(name="course_id", nullable = false)
private Course course;
}
Exercise.java
@Entity
@Table(name = "exercise")
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public class Exercise extends Activity {
@Column(name = "deadline")
private Date deadline;
@Column(name = "task", nullable = false)
private String task;
}
Contest.java
@Entity
@Table(name = "contest")
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public class Contest extends Activity {
@Column(name = "start")
private Date start;
@Column(name = "task", nullable = false)
private String task;
}
My Repository looks like this:
ActivityRepository.java
public interface ActivityRepository extends JpaRepository<Activity, Integer>{
@Query(value = "SELECT * FROM activity a WHERE course_id=:course_id", nativeQuery = true)
public List<Activity> getByCourseId(@Param("course_id") int course_id);
}
When executing the Query getByCourseId()
, I receive the SQL Error Column 'clazz_' not found
.
Don't use a native query. Try this :
public List<Activity> findByCourse(Course course);
It should work without writing the query which is :
select a from Activity where a.course = :course