Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@OrderBy not working properly in JPA

How does @OrderBy work?

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>

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




}
 }
like image 299
Shoaib Chikate Avatar asked Jun 06 '13 15:06

Shoaib Chikate


1 Answers

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.

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

Then addresses would be sorted by city in descending order.

like image 132
DannyMo Avatar answered Oct 04 '22 22:10

DannyMo