小编Cla*_*den的帖子

带有Hibernate 4和ManyToOne级联的IllegalStateException

我有两个班

MyItem对象:

@Entity
public class MyItem implements Serializable {

    @Id
    private Integer id;
    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private Component defaultComponent;
    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private Component masterComponent;

    //default constructor, getter, setter, equals and hashCode
}
Run Code Online (Sandbox Code Playgroud)

组件对象:

@Entity
public class Component implements Serializable {

    @Id
    private String name;

    //again, default constructor, getter, setter, equals and hashCode
}
Run Code Online (Sandbox Code Playgroud)

我想用以下代码坚持下去:

public class Test {

    public static void main(String[] args) {
        Component c1 = new Component();
        c1.setName("comp");
        Component c2 = new Component();
        c2.setName("comp");
        System.out.println(c1.equals(c2)); …
Run Code Online (Sandbox Code Playgroud)

java hibernate jpa many-to-one hibernate-cascade

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

在单独的行中显示每个列表元素(控制台)

在这部分代码中:

    System.out.println("Alunos aprovados:");
    String[] aprovados = {"d", "a", "c", "b"};
    List<String> list = new ArrayList();
    for (int i = 0; i < aprovados.length; i++) {
        if (aprovados[i] != null) {
            list.add(aprovados[i]);
        }
    }

    aprovados = list.toArray(new String[list.size()]);
    Arrays.sort(aprovados);
    System.out.println(Arrays.asList(aprovados));
Run Code Online (Sandbox Code Playgroud)

System.out.println的示例结果是:

[A B C D]

如果我想要下面的结果,我怎么能修改上面的代码?

一个

b

C

d

或至少:

一个,

b,

C,

d

java console newline arraylist

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

我的ThreadPoolExecutor泄漏了内存吗?

我正在使用a ThreadPoolExecutor来运行任务.后端是一个SynchronousQueue,所以如果执行程序已经执行了一个任务,它会抛出一个任务RejectedExecutionException.这是一个简单的测试用例:

public class ExecutorTest {

  final static Worker worker = new Worker();

  public static void main(String[] args) {
    ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());

    while (true) {
        try {                
            executor.execute(worker);                
        }catch (RejectedExecutionException e) {                
        }
    }        
  }

  static class Worker implements Runnable {

    private int i = 0;
    private long start = System.currentTimeMillis();

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
            System.out.println(++i + " " + (System.currentTimeMillis() - start));
        } …
Run Code Online (Sandbox Code Playgroud)

java threadpoolexecutor

8
推荐指数
1
解决办法
3860
查看次数

HQL或Criteria中的CASE语句

这个问题派生出来,是否可以将HQL或Criteria用于以下SQL语句:

SELECT 
   e.type, 
   count(e), 
   count(d), 
   count (case when gender = 'male' then 1 else NULL end) AS NumberOfMaleEmployees
from Department d 
JOIN d.employees e
WHERE e.dead = 'maybe' 
GROUP BY e.type
Run Code Online (Sandbox Code Playgroud)

虽然谷歌提出了一些HQL支持CASE语句的命中,但Hibernate 3.6.6失败了

QuerySyntaxException:意外令牌:CASE

当我在EntityManager实例上创建上面的查询时.

为每个e.type创建另一个查询来手动确定男性的数量,例如每个e.type,这有多糟糕.

SELECT 
   count(e), 
from Department d 
JOIN d.employees e
WHERE e.dead = 'maybe', e.type = ugly
Run Code Online (Sandbox Code Playgroud)

由于可能有很多类型,这可能很慢.我想数据库为我做的工作.

java sql hql criteria-api

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

使用通用 Set 接口时指定 HashSet 的类型有什么用?

如果实例化一个新的 HashSet,之后通常会使用 Set 接口来使用它。就像

Set<T> set = new HashSet();
Run Code Online (Sandbox Code Playgroud)

那么显式指定 HashSet 的类型有什么用呢?例如:

Set<T> set = new HashSet<T>();
Run Code Online (Sandbox Code Playgroud)

我在好几本书上见过它,但我根本想不出有什么用处。如果您需要访问该集合,无论如何您都将使用该界面(已参数化)。

java

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

使用递归来反转字符串的方法

public static void main(String args[]) {
    System.out.println(reverseString("His"));
}

public static String reverseString(String s) {
    if (s.length() <= 1) {
        return s;
    } else {
        char c = s.charAt(0);
        return reverseString(s.substring(1)) + c;
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以详细解释我这个方法是如何工作的

java recursion

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