@orderby在jpa中工作不正常

cgvd09ve  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(397)

你怎么知道的 @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>

这是测试类……检查城市名称,它没有在地址表中按降序存储地址值
JPAOrderByNotation测试

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();

}
 }
x759pob2

x759pob21#

我想你误解了 @Orderby 注解确实如此。根据javadoc:
指定在检索关联或集合时,集合值关联或元素集合的元素的顺序。
[强调添加]注解不指定插入顺序。继续你的例子,如果你要拿一个 Employee :

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

那么 addresses 将按 city 按降序排列。

11dmarpk

11dmarpk2#

根据规格,您必须使用:

@OrderBy("address.city DESC")

相关问题