Vaadin Web应用开发教程(49): SQLContainer-引用其它SQLContainer
数据库表之间存在参考关键,这对应到数据库通常为外键引用。Vaadin 的SQLContainer提供了不同SQLContainer之间引用的有限支持,但其实现主要是通过Java 代码来实现的,并不需要数据库的表之间一定要有外键定义。
给一个SQLContainer添加引用的方法为:
[java]
public void addReference(SQLContainer refdCont,
String refingCol, String refdCol);
public void addReference(SQLContainer refdCont,
String refingCol, String refdCol);
refdCont为被引用的SQLContainer,refingCol 为源SQLContainer对应的列名,refdCol为目标SQLContainer被参照的列名。
要注意的是SQLContainer不支持同一个SQLContainer参照自身。
还是以Customer ,Invoice 为例。 显示所有Invoice,但点击某个Invoice,显示对应的Customer的姓名。
[java]
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();
}
}
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,
&nbs
补充:Web开发 , 其他 ,