简体   繁体   中英

order in hibernate

i have this bean

public class Advertisement{
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "pkid", nullable = false)
    @Basic(fetch = FetchType.EAGER)
    private long adPkId;

    @Size(max = 50, message = "{long.value}")
    @Column(name = "Name", unique = true, nullable = false, length = 50)
    private String name;

    @Size(max = 255, message = "{long.value}")
    @Column(name = "Description", length = 255)
    private String description;
}

i want to return all data order by id

getCurrentSession().createCriteria(Advertisement.class)
                .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
                .addOrder(Order.asc("adPkId")).list();

the data in table take ids from 1 to 7 the data returned in list not order that return ids (3 - 4 - 5 - 6 - 7 - 1 - 2)

how to fix it

the problem come from

@OneToMany(fetch = FetchType.EAGER, mappedBy = "advertisement", orphanRemoval = true, cascade = CascadeType.REMOVE)
    @OrderBy("name")
    private Set<test> test= new HashSet<test>(0);

i change fetch = FetchType.EAGER to be lazy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM