- A simple entity for this example.
@Entity
@Table(name="ALERT")
public class AlertEO implements java.io.Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String description;
/**
* method to get serial Id
*
* @return id
*/
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}
/**
* Functions to get id
* @return id
*/
public void setId(Integer id){
this.id = id;
}
/**
* Functions to get name
* @return name
*/
@Column(name = "name")
public String getName(){
return name;
}
/**
* Functions to set name
* @return name
*/
public void setName(String name){
this.name = name;
}
/**
* Functions to get description
* @return description
*/
@Column(name = "description")
public String getDescription(){
return description;
}
/**
* Functions to set description
* @return description
*/
public void setDescription(String description){
this.description=description;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "AlertEO [id=" + id + ", name=" + name + ", description=" + description + "]";
}
The class of the static metamodel looks similar to the entity.
Based on the JPA specification, there is a corresponding metamodel class for every managed class in the persistence unit.
You can find it in the same package and it has the same name as the corresponding managed class with an added ‘_’ at the end
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(AlertEO.class)
public abstract class AlertEO_{
public static volatile SingularAttribute<AlertEO, String>firstName;
public static volatile SingularAttribute<AlertEO, String> lastName;
public static volatile SetAttribute<AlertEO, Book> books;
public static volatile SingularAttribute<AlertEO, Long> id;
public static volatile SingularAttribute<AlertEO, Integer> version;
}
- Using metamodel classes
- You can use the metamodel classes in the same way as you use the String reference to the entities and attributes.
- The APIs for criteria queries and dynamic entity graphs provide overloaded methods that accept Strings and implementations of the Attribute interface.
CriteriaBuilder cb = this.em.getCriteriaBuilder();
// create the query
CriteriaQuey<AlertEO> q = cb.createQuery(AlertEO.class);
// set the root class
Root<AlertEO> a = q.from(AlertEO.class);
// use metadata class to define the where clause
q.where(cb.like(a.get(AlertEO_.name), "J%"));
// perform query
this.em.createQuery(q).getResultList();
No comments :