软件介绍:本文整理了Java中org.hibernate.transform.AliasToBeanResultTransformer类的一些代码示例,展示了AliasT...
本文整理了Java中org.hibernate.transform.AliasToBeanResultTransformer类的一些代码示例,展示了AliasToBeanResultTransformer类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AliasToBeanResultTransformer类的具体详情如下:
包路径:org.hibernate.transform.AliasToBeanResultTransformer
类名称:AliasToBeanResultTransformer
AliasToBeanResultTransformer介绍
[英]Result transformer that allows to transform a result to a user specified class which will be populated via setter methods or fields matching the alias names.
List resultWithAliasedBean = s.createCriteria(Enrolment.class)
.createAlias("student", "st")
.createAlias("course", "co")
.setProjection( Projections.projectionList()
.add( Projections.property("co.description"), "courseDescription" )
)
.setResultTransformer( new AliasToBeanResultTransformer(StudentDTO.class) )
.list();
StudentDTO dto = (StudentDTO)resultWithAliasedBean.get(0);
[中]结果转换器,允许将结果转换为用户指定的类,该类将通过与别名匹配的setter方法或字段填充。
List resultWithAliasedBean = s.createCriteria(Enrolment.class)
.createAlias("student", "st")
.createAlias("course", "co")
.setProjection( Projections.projectionList()
.add( Projections.property("co.description"), "courseDescription" )
)
.setResultTransformer( new AliasToBeanResultTransformer(StudentDTO.class) )
.list();
StudentDTO dto = (StudentDTO)resultWithAliasedBean.get(0);
代码示例
/**
* Creates a resulttransformer that will inject aliased values into
* instances of Class via property methods or fields.
*/
public static ResultTransformer aliasToBean(Class target) {
return new AliasToBeanResultTransformer(target);
}
@Override
public Object transformTuple(Object[] tuple, String[] aliases) {
Object result;
try {
if ( ! isInitialized ) {
initialize( aliases );
}
else {
check( aliases );
}
result = resultClass.newInstance();
for ( int i = 0; i < aliases.length; i++ ) {
if ( setters[i] != null ) {
setters[i].set( result, tuple[i], null );
}
}
}
catch ( InstantiationException e ) {
throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
}
catch ( IllegalAccessException e ) {
throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
}
return result;
}
@Test
public void testSerializedEqualityResultTransformer() throws Exception {
// settings are lazily initialized when calling transformTuple(),
// so they have not been initialized for the following test
// (it *should* be initialized before creating a QueryKey)
doResultTransformerTest( new AliasToBeanResultTransformer( AClass.class ), false );
// initialize settings for the next test
AliasToBeanResultTransformer transformer = new AliasToBeanResultTransformer( AClass.class );
transformer.transformTuple(
new Object[] { "abc", "def" },
new String[] { "propAccessedByField", "propAccessedByMethod" }
);
doResultTransformerTest( transformer, false );
doResultTransformerTest( AliasToEntityMapResultTransformer.INSTANCE, true );
doResultTransformerTest( DistinctResultTransformer.INSTANCE, true );
doResultTransformerTest( DistinctRootEntityResultTransformer.INSTANCE, true );
doResultTransformerTest( PassThroughResultTransformer.INSTANCE, true );
doResultTransformerTest( RootEntityResultTransformer.INSTANCE, true );
doResultTransformerTest( ToListResultTransformer.INSTANCE, true );
}
@Test
public void testSerializedEqualityWithTupleSubsetResultTransfprmer() throws Exception {
doTestWithTupleSubsetResultTransformer(
new AliasToBeanResultTransformer( AClass.class ),
new String[] { "propAccessedByField", "propAccessedByMethod" }
);
doTestWithTupleSubsetResultTransformer( AliasToEntityMapResultTransformer.INSTANCE, new String[] { "a", "b" } );
doTestWithTupleSubsetResultTransformer( DistinctRootEntityResultTransformer.INSTANCE, new String[] { "a", "b" } );
doTestWithTupleSubsetResultTransformer( PassThroughResultTransformer.INSTANCE, new String[] { "a", "b" } );
doTestWithTupleSubsetResultTransformer( RootEntityResultTransformer.INSTANCE, new String[] { "a", "b" } );
// The following are not TupleSubsetResultTransformers:
// DistinctResultTransformer.INSTANCE
// ToListResultTransformer.INSTANCE
}
public Object transformTuple(Object[] tuple, String[] aliases) {
Object result;
try {
if ( ! isInitialized ) {
initialize( aliases );
}
else {
check( aliases );
}
result = resultClass.newInstance();
for ( int i = 0; i < aliases.length; i++ ) {
if ( setters[i] != null ) {
setters[i].set( result, tuple[i], null );
}
}
}
catch ( InstantiationException e ) {
throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
}
catch ( IllegalAccessException e ) {
throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
}
return result;
}
/**
* Creates a resulttransformer that will inject aliased values into
* instances of Class via property methods or fields.
*/
public static ResultTransformer aliasToBean(Class target) {
return new AliasToBeanResultTransformer(target);
}
代码示例来源:origin: org.hibernate/com.springsource.org.hibernate.core
public Object transformTuple(Object[] tuple, String[] aliases) {
Object result;
try {
if ( ! isInitialized ) {
initialize( aliases );
}
else {
check( aliases );
}
result = resultClass.newInstance();
for ( int i = 0; i < aliases.length; i++ ) {
if ( setters[i] != null ) {
setters[i].set( result, tuple[i], null );
}
}
}
catch ( InstantiationException e ) {
throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
}
catch ( IllegalAccessException e ) {
throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
}
return result;
}
String nativeSQL = "select o.id as id, o.name as objectName from MyObject";
List<MyObject> resultList = session.createSQLQuery(nativeSQL)
.addScalar("id" ,StandardBasicTypes.INTEGER)
.addScalar("objectName",StandardBasicTypes.STRING)
.setResultTransformer(new AliasToBeanResultTransformer(MyObject.class))
.list();
@Override
public Object transformTuple(Object[] tuple, String[] aliases) {
Object result;
try {
if ( ! isInitialized ) {
initialize( aliases );
}
else {
check( aliases );
}
result = resultClass.newInstance();
for ( int i = 0; i < aliases.length; i++ ) {
if ( setters[i] != null ) {
setters[i].set( result, tuple[i], null );
}
}
}
catch ( InstantiationException e ) {
throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
}
catch ( IllegalAccessException e ) {
throw new HibernateException( "Could not instantiate resultclass: " + resultClass.getName() );
}
return result;
}
List<User> users = session
.createQuery(
"SELECT userID as userID, firstName as firstName FROM Users WHERE emailID = :email_ID")
.setParameter("email_ID", emailID)
.setResultTransformer(new AliasToBeanResultTransformer(User.class)).list();
List<SumValue> jobStateViewList = (List<SumValue>)getSessionFactory().getCurrentSession()
.createSQLQuery(yourQuery)
.setResultTransformer(
new AliasToBeanResultTransformer(SumValue.class)
).list();/**
* Creates a resulttransformer that will inject aliased values into
* instances of Class via property methods or fields.
*/
public static ResultTransformer aliasToBean(Class target) {
return new AliasToBeanResultTransformer(target);
}/**
* Creates a resulttransformer that will inject aliased values into
* instances of Class via property methods or fields.
*/
public static ResultTransformer aliasToBean(Class target) {
return new AliasToBeanResultTransformer(target);
}/**
* Creates a resulttransformer that will inject aliased values into
* instances of Class via property methods or fields.
*/
public static ResultTransformer aliasToBean(Class target) {
return new AliasToBeanResultTransformer(target);
}
public static ResultTransformer aliasToBean(Class target) {
return new AliasToBeanResultTransformer(target);
}
return em.unwrap(Session.class)
.createSQLQuery("...")
.setResultTransformer(new AliasToBeanResultTransformer(MyClass.class))
.list();
criteria.setResultTransformer(new AliasToBeanResultTransformer(HostelWrapper.class));
List<HostelWrapper> hostels = criteria.list();
Hostel hostel = hostels.get(0).getHostel();
String firstName = hostels.get(0).getFirstName();
Query query = session.createSQLQuery("select s.id,e.firstName, e.middleName from SecurityPrincipals as s " +
"left join Employee as e on e.userId=s.id " +
" where s.id= :userId")
.setResultTransformer(new AliasToBeanResultTransformer(EmployeeDTO.class));
public mypersistenceclass()
{
// define constructor
public mypersistenceclass(){}
private String username;
// define setter and getter
}
public userclass()
{
...
Query query = session.createQuery("select Users.username as username ...")
.setResultTransformer(new AliasToBeanResultTransformer(mypersistenceclass.class));
...
}
session.createSQLQuery("select a,b,c from table")
.setResultTransformer(new AliasToBeanResultTransformer(myclass.class))
.list();