Specific property type with Annotations

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Specific property type with Annotations

Brandon Johnson
In this contrived example, using annotations only, is it possible to tell MyBatis the type T should be Address for this mapper method? Can I use @TypeDiscriminator for a property of the returned object somehow? 

public class TestMyBatisMapper {

@Test
public void test() throws Exception {
RTSqlSession sqlSession = RTSqlSessionFactory.getSqlSession();
Mapper mapper = sqlSession.getMapper(Mapper.class);
List<Person<Address>> sensitivies = mapper.getPeople();
System.out.println(sensitivies);

}

public interface Mapper {
@Results({
@Result(column = "city", property = "address.city"),
@Result(column = "state", property = "address.state")
})
@Select("select 'brandon' as name, 'charlotte' as city, 'NC' as state")
List<Person<Address>> getPeople();
}

public static class Person<T> {
private String name;
private T address;

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", address=" + address +
'}';
}
}

public static class Address {
private String city;
private String state;

@Override
public String toString() {
return "Address{" +
"city='" + city + '\'' +
", state='" + state + '\'' +
'}';
}
}
}

I tried adding another @Result annotation to my @Results but get an error
@Results({
@Result(property = "address", javaType = Address.class),
@Result(column = "city", property = "address.city"),
@Result(column = "state", property = "address.state")
})

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Specific property type with Annotations

Brandon Johnson
I should have added, that this code currently complains about missing city and state properties on java.lang.Object.

On Monday, August 8, 2016 at 7:00:57 PM UTC-4, Brandon Johnson wrote:
In this contrived example, using annotations only, is it possible to tell MyBatis the type T should be Address for this mapper method? Can I use @TypeDiscriminator for a property of the returned object somehow? 

public class TestMyBatisMapper {

@Test
public void test() throws Exception {
RTSqlSession sqlSession = RTSqlSessionFactory.getSqlSession();
Mapper mapper = sqlSession.getMapper(Mapper.class);
List<Person<Address>> sensitivies = mapper.getPeople();
System.out.println(sensitivies);

}

public interface Mapper {
@Results({
@Result(column = "city", property = "address.city"),
@Result(column = "state", property = "address.state")
})
@Select("select 'brandon' as name, 'charlotte' as city, 'NC' as state")
List<Person<Address>> getPeople();
}

public static class Person<T> {
private String name;
private T address;

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", address=" + address +
'}';
}
}

public static class Address {
private String city;
private String state;

@Override
public String toString() {
return "Address{" +
"city='" + city + '\'' +
", state='" + state + '\'' +
'}';
}
}
}

I tried adding another @Result annotation to my @Results but get an error
@Results({
@Result(property = "address", javaType = Address.class),
@Result(column = "city", property = "address.city"),
@Result(column = "state", property = "address.state")
})

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.