/*
* Copyright 2014 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http: *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.hibernate.bugs;
import org.hibernate.annotations.Formula;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
import javax.persistence.*;
import java.util.List;
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
import static org.junit.Assert.assertEquals;
/**
* This template demonstrates how to develop a test case for Hibernate ORM, using its built-in unit test framework.
* Although ORMStandaloneTestCase is perfectly acceptable as a reproducer, usage of this class is much preferred.
* Since we nearly always include a regression test with bug fixes, providing your reproducer using this method
* simplifies the process.
* <p>
* What's even better? Fork hibernate-orm itself, add your test case directly to a module's unit tests, then
* submit it as a PR!
*/
public class HHH7525TestCase extends BaseCoreFunctionalTestCase {
@Override
protected Class[] getAnnotatedClasses() {
return new Class[]{
Foo.class
};
}
@Override
protected String[] getMappings() {
return new String[]{};
}
@Override
protected String getBaseForMappings() {
return "org/hibernate/test/";
}
@Override
protected void configure(Configuration configuration) {
super.configure(configuration);
configuration.setProperty(AvailableSettings.SHOW_SQL, Boolean.TRUE.toString());
configuration.setProperty(AvailableSettings.FORMAT_SQL, Boolean.TRUE.toString());
}
@Test
public void HHH7525Test() throws Exception {
doInHibernate(this::sessionFactory, session -> {
for (long i = 0; i < 10; i++) {
Foo foo = new Foo();
foo.setName("name");
foo.setCode("c" + i);
session.save(foo);
}
});
doInHibernate(this::sessionFactory, session -> {
Query query = session.createNativeQuery("SELECT ft.* FROM foo_table ft", Foo.class);
List<Foo> list = query.getResultList();
assertEquals(10, list.size());
});
}
@Entity
@Table(name = "foo_table")
public static class Foo {
private String name;
private String code;
private String fullName;
@Column
public String getName() {
return name;
}
@Id
@Column
public String getCode() {
return code;
}
@Formula("name&' '&code")
public String getFullName() {
return fullName;
}
public void setName(String name) {
this.name = name;
}
public void setCode(String code) {
this.code = code;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
}
}