小编Ani*_*ish的帖子

在java中编写泛型函数

我想写一个函数,它会返回一个我传递给它的指定类型的Vector.例如:

// Here getNewVector(<ClassType>) should return me an integer Vector.
Vector<Integer> myIntVector = getNewVector(Integer.class);
//should get a String vector in this case and so on.
Vector<String> myStringVector = getNewVector(String.class) ; 
Run Code Online (Sandbox Code Playgroud)

我想以getVector(Class classType)这种方式实现,以便返回该特定类类型的新向量.如何在不使用反射的情况下实现它,并且不将类名作为String传递(我只想传递类类型,就像我在上面的例子中提到的那样.)

实际上,我想要一个函数getVector()有点像这样..

Vector<T> getVector(T t) {
    return new Vector<t>();
}
Run Code Online (Sandbox Code Playgroud)

java generics

3
推荐指数
1
解决办法
259
查看次数

使用GSon将hibernate对象转换为json

我试图将一个集合(从Hibernate获得)转换为使用Gson的json字符串,用于我的restful服务.但是,该服务给了我http 500错误.

我的代码如下:

@GET
@Path("/getScheduleLookUp")
@Produces(MediaType.APPLICATION_JSON)
public String getScheduleLookUp() throws IOException {
    ScheduleLookupDAO lookupDAO = new ScheduleLookupDAO();
    return new Gson().toJson(lookupDAO.getAllScheduleLookUps());
}
Run Code Online (Sandbox Code Playgroud)

查询数据库后返回的hibernate列表返回带有一些垃圾值的classname,可能是因为Hibernate的延迟加载.我尝试通过关闭延迟加载,但最终得到了相同的结果.我的班级结构有点像这样

public class ScheduleLookup extends BaseModel {

private static final long serialVersionUID = -2561410864465885712L;

private Integer scheduleId;
private String scheduleName;
private ClientLookup clientLookup;
private String description;
private Route route;
private String routeName;
private Double estimatedDuration;
private Double calculatedDuration;
private UserLookup createdBy;
private Date createdAt;
private UserLookup lastUpdatedBy;
private Date lastUpdatedAt;
private Boolean delFlag;
private UserLookup deletedBy;
private Date deletedAt; …
Run Code Online (Sandbox Code Playgroud)

java json hibernate gson

3
推荐指数
1
解决办法
2万
查看次数

Spring data cassandra 的事务管理

我使用 Spring 和 Cassandra 作为底层数据库。曾提到过 spring 伞项目 'spring data cassandra'。与休眠不同,无法找出此处如何管理事务。如果你们中的一些人已经合并了交易管理器,请分享交易管理器的详细信息。

java spring spring-data-cassandra

2
推荐指数
2
解决办法
5797
查看次数

标签 统计

java ×3

generics ×1

gson ×1

hibernate ×1

json ×1

spring ×1

spring-data-cassandra ×1