我有一些相关的条款,我希望得到一个简单的选择,如果是用sql
我只想要制作SELECT * FROM person WHERE id_person_type = 1
但是hibernate给我带来了另一种选择
我使用的是版本4.3.5.Final
以下是我的变量,其变量少于原始变量,但有关系:
@Entity
@Table(name="person"
,catalog="htlsys"
)
public class Person implements java.io.Serializable {
private static final long serialVersionUID = -5504579691788263257L;
private Integer idPerson;
@NotNull
private PersonType personType;
@NotNull
@Valid
private Address address;
@NotNull
@Size(min = 2, max = 20)
private String firstName;
@NotNull
@Valid
private Set<PersonPhone> personPhones = new HashSet<PersonPhone>(0);
public Person() {
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="id_person", unique=true, nullable=false)
public Integer getIdPerson() {
return this.idPerson;
}
public void setIdPerson(Integer idPerson) {
this.idPerson = idPerson;
}
@OneToOne(fetch=FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinColumn(name="id_person_type", nullable=false)
public PersonType getPersonType() {
return this.personType;
}
public void setPersonType(PersonType personType) {
this.personType = personType;
}
@OneToOne(fetch=FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinColumn(name="id_address")
public Address getAddress() {
return this.address;
}
public void setAddress(Address address) {
this.address = address;
}
@Column(name="first_name", length=20)
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@OneToMany(fetch=FetchType.EAGER, mappedBy="person", cascade = {CascadeType.ALL})
public Set<PersonPhone> getPersonPhones() {
return this.personPhones;
}
public void setPersonPhones(Set<PersonPhone> personPhones) {
this.personPhones = personPhones;
}
}
@Entity
@Table(name="person_type"
,catalog="htlsys"
)
public class PersonType implements java.io.Serializable {
private static final long serialVersionUID = 2689950061291402765L;
private Integer idPersonType;
@NotNull
@Size(min = 0, max = 15)
private String name;
private Set<Person> persons = new HashSet<Person>(0);
public PersonType() {
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="id_person_type", unique=true, nullable=false)
public Integer getIdPersonType() {
return this.idPersonType;
}
public void setIdPersonType(Integer idPersonType) {
this.idPersonType = idPersonType;
}
@Column(name="name", nullable=false, length=15)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="personType")
public Set<Person> getPersons() {
return this.persons;
}
public void setPersons(Set<Person> persons) {
this.persons = persons;
}
}
@Entity
@Table(name="address"
,catalog="htlsys"
)
public class Address implements java.io.Serializable {
private static final long serialVersionUID = -3206021001369009888L;
private Integer idAddress;
@NotNull
@Size(min = 1, max = 60)
private String street;
private Set<Person> persons = new HashSet<Person>(0);
public Address() {
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="id_address", unique=true, nullable=false)
public Integer getIdAddress() {
return this.idAddress;
}
public void setIdAddress(Integer idAddress) {
this.idAddress = idAddress;
}
@Column(name="street", nullable=false, length=60)
public String getStreet() {
return this.street;
}
public void setStreet(String street) {
this.street = street;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="address")
public Set<Person> getPersons() {
return this.persons;
}
public void setPersons(Set<Person> persons) {
this.persons = persons;
}
}
@Entity
@Table(name="person_phone"
,catalog="htlsys"
)
public class PersonPhone implements java.io.Serializable {
private static final long serialVersionUID = 3677299664669135022L;
private Integer idPersonPhone;
private Person person;
private PhoneNumber phoneNumber;
public PersonPhone() {
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="id_person_phone", unique=true, nullable=false)
public Integer getIdPersonPhone() {
return this.idPersonPhone;
}
public void setIdPersonPhone(Integer idPersonPhone) {
this.idPersonPhone = idPersonPhone;
}
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="id_person")
public Person getPerson() {
return this.person;
}
public void setPerson(Person person) {
this.person = person;
}
@ManyToOne(fetch=FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinColumn(name="id_phone")
public PhoneNumber getPhoneNumber() {
return this.phoneNumber;
}
public void setPhoneNumber(PhoneNumber phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
@Entity
@Table(name="phone_number"
,catalog="htlsys"
)
public class PhoneNumber implements java.io.Serializable {
private static final long serialVersionUID = 8490149919691287421L;
private Integer idPhoneNumber;
private PhoneType phoneType;
@NotNull
@Size(min = 1, max = 20)
private String number;
private Set<PersonPhone> personPhones = new HashSet<PersonPhone>(0);
public PhoneNumber() {
}
public PhoneNumber(String number) {
this.number = number;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="id_phone_number", unique=true, nullable=false)
public Integer getIdPhoneNumber() {
return this.idPhoneNumber;
}
public void setIdPhoneNumber(Integer idPhoneNumber) {
this.idPhoneNumber = idPhoneNumber;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="id_phone_type")
public PhoneType getPhoneType() {
return this.phoneType;
}
public void setPhoneType(PhoneType phoneType) {
this.phoneType = phoneType;
}
@Column(name="number", nullable=false, length=20)
public String getNumber() {
return this.number;
}
public void setNumber(String number) {
this.number = number;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="phoneNumber")
public Set<PersonPhone> getPersonPhones() {
return this.personPhones;
}
public void setPersonPhones(Set<PersonPhone> personPhones) {
this.personPhones = personPhones;
}
}
@Entity
@Table(name="phone_type"
,catalog="htlsys"
)
public class PhoneType implements java.io.Serializable {
private static final long serialVersionUID = -7703414197297512412L;
private Integer idPhoneType;
@NotNull
@Size(min = 0, max = 20)
private String nameType;
private Set<PhoneNumber> phoneNumbers = new HashSet<PhoneNumber>(0);
public PhoneType() {
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="id_phone_type", unique=true, nullable=false)
public Integer getIdPhoneType() {
return this.idPhoneType;
}
public void setIdPhoneType(Integer idPhoneType) {
this.idPhoneType = idPhoneType;
}
@Column(name="name_type", length=20)
public String getNameType() {
return this.nameType;
}
public void setNameType(String nameType) {
this.nameType = nameType;
}
@OneToMany(fetch=FetchType.LAZY, mappedBy="phoneType")
public Set<PhoneNumber> getPhoneNumbers() {
return this.phoneNumbers;
}
public void setPhoneNumbers(Set<PhoneNumber> phoneNumbers) {
this.phoneNumbers = phoneNumbers;
}
}
标准:
Criteria criteria = getCurrentSession().createCriteria(entityClass);
criteria.add(Restrictions.eq("personType.idPersonType", 1));
criteria.addOrder(Order.asc("surname"));
List<Person> list = criteria.list();
答案 0 :(得分:1)
criteria.add(Restrictions.eq("personType.idPersonType", 1));
criteria.addOrder(Order.asc("surname"))
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);