小编Kon*_*kov的帖子

如何使用Hibernate实现从数据库中获取数据的通用方法?

我有添加,更新和删除实体的通用方法.但我也想使用泛型来检索数据.

这是我获取数据的方法:

public static List<ClassA> getAllClassAData() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(ClassA.class);
    return criteria.list();
}

public static List<ClassB> getAllClassBData() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(ClassB.class);
    return criteria.list();
}

public static List<ClassC> getAllClassCData() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(ClassC.class);
    return criteria.list();
}
Run Code Online (Sandbox Code Playgroud)

我试着做这样的事情:

public static <T> List<T> getAllData() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Criteria criteria = session.createCriteria(???.class); //Here is where I'm stuck
    return criteria.list();
}
Run Code Online (Sandbox Code Playgroud)

我坚持将类作为arg传递给标准创建:

 Criteria criteria = session.createCriteria(Generic.class);
Run Code Online (Sandbox Code Playgroud)

如何让JVM知道我在标准创建过程中想要传递的类?

java generics hibernate

4
推荐指数
1
解决办法
1727
查看次数

包括<noscript>标签中的php

是否可以在<noscript>标签中包含一个php文件?......以显示javascript是否被禁用?

php

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

对私人和公共使用感到困惑

我在我的代码中遇到私有LinkedList股票的错误.

错误表明我有一个非法的表达开始,可能是私人的?如何更改此以消除错误?如果我将其更改为,我会得到完全相同的错误public.我stockItem班上的所有东西也都被设定了public.

任何帮助将非常感激

import java.util.*;
public class StockList {
{
 private LinkedList<StockItem> stock
        = new LinkedList<StockItem>();


public StockList() {};
Run Code Online (Sandbox Code Playgroud)

谢谢.

java private

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

java.lang.ClassCastException:java.lang.String无法强制转换为java.lang.Long

我创建了这个用于读取Linux正常运行时间的简单示例:

public String getMachineUptime() throws IOException {
    String[] dic = readData().split(" ");

    long s = (long) Array.get(dic, 1);
    return calculateTime(s);
}

private String readData() throws IOException {
    byte[] fileBytes;
    File myFile = new File("/proc/uptime");
    if (myFile.exists()) {
        try {
            fileBytes = Files.readAllBytes(myFile.toPath());
        } catch (java.nio.file.AccessDeniedException e) {
            return null;
        }

        if (fileBytes.length > 0) {
            return new String(fileBytes);
        }
    }
    return null;
}

private String calculateTime(long seconds) {
    int day = (int) TimeUnit.SECONDS.toDays(seconds);
    long hours = TimeUnit.SECONDS.toHours(seconds)
            - TimeUnit.DAYS.toHours(day);
    long …
Run Code Online (Sandbox Code Playgroud)

java

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

为什么这是错的?关于java 8流媒体

public interface Filter<M> {

    boolean match(M m);

    public static <T> Collection<T> filter(Collection<T> collection, Filter<T> filter) {
        return collection.stream().filter(filter::match).collect(Collectors.toList());
    }

    ////////////////////////////////////////////////////////////////

    public static void main(String[] args) {
        ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8));
        System.out.println(intList);

        List<Integer> list = filter(intList, null);

        System.out.println(list);
    }
}
Run Code Online (Sandbox Code Playgroud)

参数类型错误

我正在学习java 8流媒体功能,这是我有问题的代码......

我不知道为什么参数intList不符合filter()方法.Java应该知道<T>Integer这里,对吗?

java collections java-8

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

替换包含"with \"的字符串?

我怎么能代替包含字符串"\"

replace(""","\"") 不适合我.

public static String replaceSpecialCharsForJson(String str){
    return str.replace("'","\'")
              .replace(":","\\:")
              .replace("\"","\"")
              .replace("\r", "\\r")
              .replace("\n", "\\n");
} 
Run Code Online (Sandbox Code Playgroud)

java string

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

在类泛型中使用任意数量的参数 - Java

我想知道是否可以在类泛型中使用任意数量的参数?在方法中,可以使用'...'这就是为什么我想知道在类泛型中是否有类似的方法可以做到这一点

Class<T ...>
Run Code Online (Sandbox Code Playgroud)

像上面那样的东西.

java generics

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

如何在我的java类中显示卢比符号?

if(this.currency.equalsIgnoreCase("?")) {
    r="? "+r;
}
Run Code Online (Sandbox Code Playgroud)

我想?在java中比较(印度卢比符号).在这里,我试图打印货币卢比符号,但它显示的符号就像â?¹.

如何克服这个问题?

java

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

在java中使用类型参数进行转换

说我有这样一个类:

class ParameterizedType<T>{
    public boolean equals(Object o){
        if (ParameterizedType<T>.class.isInstance(o)){
            ParameterizedType<T> other = (ParameterizedType<T>) o;
            return this.toString() == other.toString();
        }
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

在这种方法中,我可以从eclipse中得到两个不同的警告.

  1. ParameterizedType<T>.class 在语法上是不正确的

  2. (ParameterizedType<T>) o是一个未经检查的演员

怎么可以解决这个问题呢?

java generics

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

HashSet包含不适用于Integer

我不明白为什么包含(如果我在自定义类已经过去了,我可以重新审视我的hascode和equals方法,但这是整型其实)不工作.那么代替包含我可以使用的东西?请帮忙.

Set<Integer> st = new HashSet<>();
st.add(12);
Set<Integer> st1 = new HashSet<>();
st1.add(12);
System.out.println(st.contains(st1));
Run Code Online (Sandbox Code Playgroud)

java

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

标签 统计

java ×9

generics ×3

collections ×1

hibernate ×1

java-8 ×1

php ×1

private ×1

string ×1