Vaadin Web应用开发教程(49): SQLContainer-引用其它SQLContainer

51 篇文章 0 订阅
50 篇文章 0 订阅
数据库表之间存在参考关键,这对应到数据库通常为外键引用。Vaadin 的SQLContainer提供了不同SQLContainer之间引用的有限支持,但其实现主要是通过Java 代码来实现的,并不需要数据库的表之间一定要有外键定义。

给一个SQLContainer添加引用的方法为:

public void addReference(SQLContainer refdCont,
                         String refingCol, String refdCol);

refdCont为被引用的SQLContainer,refingCol 为源SQLContainer对应的列名,refdCol为目标SQLContainer被参照的列名。
要注意的是SQLContainer不支持同一个SQLContainer参照自身。

还是以Customer ,Invoice 为例。 显示所有Invoice,但点击某个Invoice,显示对应的Customer的姓名。

void openTable(VerticalLayout layout){
	try {
		JDBCConnectionPool pool = new SimpleJDBCConnectionPool(
				"org.hsqldb.jdbc.JDBCDriver",
				"jdbc:hsqldb:file:/hsqldb/data/sample", "SA", "", 2, 5);
		TableQuery customers = new TableQuery("CUSTOMER", pool);
		customers.setVersionColumn("OPTLOCK");
		TableQuery invoices = new TableQuery("INVOICE", pool);
		customers.setVersionColumn("OPTLOCK");
		final SQLContainer customerContainer
                        = new SQLContainer(customers);
		final SQLContainer invoiceContainer
                       = new SQLContainer(invoices);
		Table table = new Table("All Invoices", invoiceContainer);
		table.setSelectable(true);

		// Send changes in selection immediately to server.
		table.setImmediate(true);

		invoiceContainer.addReference(customerContainer, 
                           "CUSTOMERID", "ID");
		
		table.addListener(new ItemClickListener(){

			public void itemClick(ItemClickEvent event) {
				RowItem rowItem=(RowItem)event.getItem();
						
				RowItem customerItem
					=(RowItem)invoiceContainer
					.getReferencedItem(rowItem.getId(), 
							customerContainer);
				
				customerLabel.setValue(customerItem
                               .getItemProperty("FIRSTNAME")
                               .toString()
                                +" "+ customerItem
                                .getItemProperty("LASTNAME")
                                .toString());
				
			}});
		layout.addComponent(table);
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
}

SQLContainer和参照其它SQLContainer的主要方法如下:

public boolean setReferencedItem(Object itemId,
        Object refdItemId, SQLContainer refdCont)
public Object getReferencedItemId(Object itemId,
                                  SQLContainer refdCont)
public Item getReferencedItem(Object itemId,
                              SQLContainer refdCont)
public boolean removeReference(SQLContainer refdCont)
这里不再一一说明了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值