Answer the question
In order to leave comments, you need to log in
How to use the JPA @Query annotation?
package Service;
import Entitys.Contact;
import org.jboss.logging.Param;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.scheduling.annotation.Async;
/**
* Created by D$DA$E on 14.01.2017.
*/
public interface ContactRepository extends JpaRepository<Contact,Long> {
@Query("select d from Entitys.Contact d where d.id = :id")
Contact findById(@Param("id") Long id);
}
package Entitys;
import org.hibernate.annotations.Type;
import org.springframework.format.annotation.DateTimeFormat;
import javax.persistence.*;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import static javax.persistence.GenerationType.IDENTITY;
/**
* Created by D$DA$E on 14.01.2017.
*/
@Entity
@Table(name = "contact")
public class Contact implements Serializable {
private Long id;
private int version;
private String firstName;
private String lastName;
private Date birthDate;
private String description;
private byte[] photo;
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "ID")
public Long getid() {return id;}
public void setid (Long id) {this. id = id;}
@Version
@Column(name = "VERSION")
public int getVersion() {return version;}
public void setVersion(int version) {this.version = version;}
@Column(name = "FIRST_NAJ1E")
public String getFirstName() {return firstName;}
public void setFirstName(String firstName) {this.firstName = firstName;}
@Column(name = "LAST_NAМE")
public String getLastName() {return lastName;}
public void setLastName(String lastName) {this.lastName = lastName;}
@Column(name = "BIRTH_DATE")
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@DateTimeFormat(iso= DateTimeFormat.ISO.DATE)
public Date getBirthDate() {return birthDate;}
public void setBirthDate(Date birthDate) {this.birthDate = birthDate;}
@Column (name = "DESCRIPTION")
public String getDescription() {return description;}
public void setDescription(String description) {this.description = description;}
@Basic(fetch= FetchType.LAZY)
@Lob
@Column(name = "РНОТО")
public byte [] getPhoto () {return photo;}
public void setPhoto (byte [] photo) {this.photo = photo;}
@Transient
public String getBirthDateString() {
String birthDateString = "";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
if (birthDate != null)
birthDateString = simpleDateFormat.format(birthDate);
return birthDateString;
}
@Override
public String toString(){
return "Contact - Id: " + id + ", First name: " + firstName
+ ", Last name: " + lastName + ", Birthday: " + birthDate
+ ", Description: " + description;}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question