2012-03-04 43 views
0

帮助为下一个对象创建Table模型。 假设有一类jTable和Hibernate

public class Class1 implements Serializable { 

    private Long id = null; 
    private String name = null; 
    private Set <Class2> transaction = new HashSet <Class2>(); 

    get and set ... 
} 

public class Class2 implements Serializable { 

    private Long class1Id = null; 
    private String field1 = null; 
    private Class1 class1 = null; 

    get and set ... 
} 

一对多。 该表显示ID,名称,field1。 1,“约翰”,asd; 1,“约翰”,2; ....这样的事情。举例说明什么可以是什么?

+0

你的问题是什么?你有什么尝试? Hibernate与这个问题有什么关系? – 2012-03-04 08:17:31

+0

我说10中的每个对象的数量都可以是其他任何数量的对象。如何将所有这些线条带到桌子上?对不起,我的英文 – user970359 2012-03-04 08:20:47

回答

1

创建一个类Class1WithTransaction包含一个Class1的实例和一个Class2的实例。 遍历Class1的实例,然后在它的每一个交易,并填充List<Class1WithTransaction>

List<Class1WithTransaction> list = new ArrayList<Class1WithTransaction>(); 
for (Class1 c1 : theObjects) { 
    if (c1.getTransactions().isEmpty()) { 
     list.add(new Class1WithTransaction(c1, null)); 
    } 
    else { 
     for (Class2 transaction : c1.getTransactions()) { 
      list.add(new Class1WithTransaction(c1, transaction)) 
     } 
    } 
} 

一旦你有了这个列表中,你只需要创建一个围绕它的表模型。该表的每一行都是Class1WithTransaction的一个实例。

+0

thx,试着去做 – user970359 2012-03-04 09:13:16