We're updating the issue view to help you get more done. 

@OrderBy in combination with @Formula fails

Description

The @OrderBy Annotion and a OneToMany association leads to a an java.lang.StringIndexOutOfBoundsException while creating the PersistenceUnit when used with @Formula on the other side:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 // EdifactRechnung @OneToMany(mappedBy="rechnung", cascade={CascadeType.ALL}, orphanRemoval=true, targetEntity=EdifactRechnungsposition.class) @OrderBy("numericPositionsnr, positionsnr") private Set<EdifactRechnungsposition> positionen = new HashSet<>(); // EdifactRechnungsposition @Column(name="positionsnr", nullable=false) @Size(max=8) @NotBlank private String positionsnr; @Basic(fetch=FetchType.LAZY) @Formula("(select to_number(positionsnr) from dual)") private int numericPositionsnr;

"to_number" is replaced with a custom function that doesn't trigger an oracle error when positionsnr isn't a number but returns 0.

Stacktrace is:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 Caused by: org.hibernate.HibernateException: Unable to parse order-by fragment at org.hibernate.sql.ordering.antlr.OrderByFragmentTranslator.translate(OrderByFragmentTranslator.java:69) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final] at org.hibernate.sql.Template.translateOrderBy(Template.java:721) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final] at org.hibernate.persister.collection.AbstractCollectionPersister.<init>(AbstractCollectionPersister.java:565) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final] at org.hibernate.persister.collection.OneToManyPersister.<init>(OneToManyPersister.java:86) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final] at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_07] at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_07] at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_07] at java.lang.reflect.Constructor.newInstance(Constructor.java:525) ~[na:1.7.0_07] at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:231) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final] ... 70 common frames omitted Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: 45 at java.lang.String.substring(String.java:1907) ~[na:1.7.0_07] at org.hibernate.sql.ordering.antlr.OrderByFragmentParser.adjustTemplateReferences(OrderByFragmentParser.java:243) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final] at org.hibernate.sql.ordering.antlr.OrderByFragmentParser.processSqlValueReference(OrderByFragmentParser.java:216) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final] at org.hibernate.sql.ordering.antlr.OrderByFragmentParser.resolveIdent(OrderByFragmentParser.java:194) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final] at org.hibernate.sql.ordering.antlr.GeneratedOrderByFragmentParser.expression(GeneratedOrderByFragmentParser.java:595) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final] at org.hibernate.sql.ordering.antlr.GeneratedOrderByFragmentParser.sortKey(GeneratedOrderByFragmentParser.java:325) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final] at org.hibernate.sql.ordering.antlr.GeneratedOrderByFragmentParser.sortSpecification(GeneratedOrderByFragmentParser.java:241) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final] at org.hibernate.sql.ordering.antlr.GeneratedOrderByFragmentParser.orderByFragment(GeneratedOrderByFragmentParser.java:190) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final] at org.hibernate.sql.ordering.antlr.OrderByFragmentTranslator.translate(OrderByFragmentTranslator.java:63) ~[hibernate-core-4.1.7.Final.jar:4.1.7.Final] ... 78 common frames omitted

This has been working since Hibernate 3.6 up to and including 4.1.4.

Environment

Oracle 10g

Status

Assignee

Brett Meyer

Reporter

Michael Simons

Fix versions

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Affects versions

4.1.5
4.1.7
4.1.6
4.1.5.SP1

Priority

Critical