@OrderBy 在 JPA 中无法正常工作 [英] @OrderBy not working properly in JPA

查看:39
本文介绍了@OrderBy 在 JPA 中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@OrderBy 如何工作?

它在以下代码中不起作用:

It is not working here in the following code:

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表中按降序存储地址值

This is the test class......check out the city name and it is not storing address values in order in descending order in the ADDRESS table

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




}
 }

推荐答案

我认为您误解了 @Orderby 注释的实际作用.根据javadoc:

I think you're misunderstanding what the @Orderby annotation actually does. According to the javadoc:

指定值集合的元素的顺序关联或元素集合在关联或集合被检索.

Specifies the ordering of the elements of a collection valued association or element collection at the point when the association or collection is retrieved.

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

[emphasis added] The annotation does not dictate insertion order. Continuing with your example, if you were to fetch an Employee:

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

然后 addresses 将按 city 降序排序.

Then addresses would be sorted by city in descending order.

这篇关于@OrderBy 在 JPA 中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆