@OrderBy在JPA中无法正常工作

时间:2013-06-06 15:17:11

标签: java jpa annotations

@OrderBy如何运作?

以下代码无效:

Employee.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;
}

  }

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;
 }

}

的persistence.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();




}
 }

2 个答案:

答案 0 :(得分:20)

我认为你误解了@Orderby注释实际上做了什么。根据{{​​3}}:

  

指定值集合的元素的顺序   关联或关联时的关联或元素集合   收集检索

[强调添加]注释不指示插入顺序。继续您的示例,如果您要获取Employee

Employee employee = manager.find(Employee.class, employeeId);
List<Address> addresses = employee.getAddress(); 

然后addresses将按city降序排序。

答案 1 :(得分:-2)

根据您必须使用的规范:

@OrderBy("address.city DESC")