I want to write a query like SELECT * FROM Release_date_type a LEFT JOIN cache_media b on a.id=b.id
. I am new to Spring Data JPA. I don't know how to write entities for Join query. Here is an attempt:
@Entity
@Table(name = "Release_date_type")
public class ReleaseDateType {
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
private Integer release_date_type_id;
// ...
@Column(nullable = true)
private Integer media_Id;
// with getters and setters...
}
Another entity is:
@Entity
@Table(name = "Cache_Media")
public class CacheMedia {
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
private Integer id;
// ...
private Date loadDate; //with the getter and setter ..
}
I want to write a crudRepository
interface such as
public interface ReleaseDateTypeRepository extends CrudRepository<ReleaseDateType, Long>{
@Query("SELECT * FROM Release_date_type a LEFT JOIN cache_media b on a.id=b.id")
public List<ReleaseDateType> FindAllWithDescriptionQuery();
}
For a typical example of employees owning one or more phones, see this wikibook section.
For your specific example, if you want to do a
one-to-one
relationship, you should change the next code in ReleaseDateType model:for:
and in CacheMedia model you need to add:
then in your repository you should replace:
by:
or by:
Or if you prefer to do a
@OneToMany
and@ManyToOne
relation, you should change the next code in ReleaseDateType model:for:
and in CacheMedia model you need to add:
then in your repository you should replace:
by:
or by: