如何@OrderBy
工作?
它在以下代码中不起作用:
雇员.java
package com.semanticbits.pojo;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int employeeId;
private String name;
private double salary;
@OneToMany(cascade=CascadeType.ALL)
@JoinColumn(name="EMP_ID")
@OrderBy("city DESC")
private List<Address> address;
//setters and getters
public int getEmployeeId() {
return employeeId;
}
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public List<Address> getAddress() {
return address;
}
public void setAddress(List<Address> address) {
this.address = address;
}
}
地址.java
package com.semanticbits.pojo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Address {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int addressId;
private String street;
private String city;
private String state;
private int zipCode;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public int getZipCode() {
return zipCode;
}
public void setZipCode(int zipCode) {
this.zipCode = zipCode;
}
}
持久性.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="orderbyannotationdemo" transaction-type="RESOURCE_LOCAL">
<provider></provider>
<class>com.semanticbits.pojo.Employee</class>
<class>com.semanticbits.pojo.Address</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/shoaib"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
这是测试类......检查城市名称,它没有在 ADDRESS 表中按降序顺序存储地址值
JPAOrderByAnnotationTest
package com.semanticbits.test;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import com.semanticbits.pojo.Address;
import com.semanticbits.pojo.Employee;
public class JPAOrderByAnnotationTest {
/**
* @param args
*/
public static void main(String[] args) {
EntityManagerFactory factory=Persistence.createEntityManagerFactory("orderbyannotationdemo");
EntityManager manager=factory.createEntityManager();
Employee employee=new Employee();
employee.setName("Shoaib");
employee.setSalary(1452365);
Address addressOffice=new Address();
addressOffice.setCity("Hyderabad");
addressOffice.setStreet("Gachibowli");
addressOffice.setState("AP");
addressOffice.setZipCode(500016);
Address addressHome=new Address();
addressHome.setCity("Noida");
addressHome.setStreet("Chandai Chowk");
addressHome.setState("UP");
addressHome.setZipCode(415608);
Address addressCollege=new Address();
addressCollege.setCity("Antartica");
addressCollege.setState("Canada");
addressCollege.setStreet("New York");
addressCollege.setZipCode(402103);
List<Address> addresses=new ArrayList<Address>();
addresses.add(addressHome);
addresses.add(addressOffice);
addresses.add(addressCollege);
employee.setAddress(addresses);
manager.getTransaction().begin();
manager.persist(employee);
manager.getTransaction().commit();
manager.close();
}
}