213 shaares
1 résultat
taggé
example
Dépendances nécessaires :
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>compile</scope>
</dependency>
<!-- Spring DATA -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>compile</scope>
</dependency>
<!-- Hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>
Définition de l'entité Hibernate :
import java.io.Serializable;
import javax.persistence.*;
import org.hibernate.annotations.GenericGenerator;
/**
* Represents a truc in the database.
*/
@Entity
@Table(name = "TRUCS")
public class Truc implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "increment")
@GenericGenerator(name = "increment", strategy = "increment")
@Column(name = "id", nullable = false, unique = true)
private Long id;
@Column(name = "name")
private String name;
/**
* Default constructor mandatory for hibernate instanciation.
*/
public Truc() {
super();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Définition du CRUD Manager :
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
/**
* The technical manager able to make CRUD actions on the truc in the database.
*/
public interface ContactManager extends CrudRepository<Truc, Long> {
/**
* Get all the trucs in the database with the given name.
*
* @param name the name.
* @return the list of all trucs having the name.
*/
@Query("select c from Truc c where lower(c.name) = lower(:name)")
List<Truc> getByName(@Param("name") String name);
}
On utilise les managers via une injection autowired et un scan du package contenant les managers.