segunda-feira, abril 04, 2005

Bean / dto com implementacao generecia toString

public class BaseDto implements Serializable{

protected void appendAttributes(Object obj, StringBuffer buffer) {
if (obj == null) {
return;
}

try {
Class clazz = obj.getClass();
Field[] fields = clazz.getFields();

for (int i = 0; i < fields.length; i++) {
buffer.append(" | " + fields[i].getName());

try {
buffer.append(" [" + fields[i].get(obj) + "]");
} catch (Exception e) {
buffer.append(" [?]");
}
}
} catch (Exception e) {}
}

public String toString() {
StringBuffer result = new StringBuffer(getClass().getName());
result.append("@" + hashCode());
appendAttributes(this, result);
return result.toString();
}
}

sexta-feira, abril 01, 2005

Exemplo de hibernate many-to-many com mysql


----
<hibernate-mapping>

<class name="Customer" table="customers">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
<set name="orders" inverse="true" lazy="true">
<key column="customer_id"/>
<one-to-many class="Order"/>
</set>
</class>

<class name="Order" table="orders">
<id name="id">
<generator class="native"/>
</id>
<property name="date"/>
<many-to-one name="customer" column="customer_id"/>
<list name="lineItems" table="line_items" lazy="true">
<key column="order_id"/>
<index column="line_number"/>
<composite-element class="LineItem">
<property name="quantity"/>
<many-to-one name="product" column="product_id"/>
</composite-element>
</list>
</class>

<class name="Product" table="products">
<id name="id">
<generator class="native"/>
</id>
<property name="serialNumber"/>
</class>

</hibernate-mapping>

---
create table works (
id BIGINT not null generated by default as identity,
tempo FLOAT,
genre VARCHAR(255),
text INTEGER,
title VARCHAR(255),
type CHAR(1) not null,
primary key (id)
)

create table author_work (
author_id BIGINT not null,
work_id BIGINT not null,
primary key (work_id, author_id)
)

create table authors (
id BIGINT not null generated by default as identity,
alias VARCHAR(255),
primary key (id)
)

create table persons (
id BIGINT not null generated by default as identity,
name VARCHAR(255),
primary key (id)
)

alter table authors
add constraint authorsFK0 foreign key (id) references persons
alter table author_work
add constraint author_workFK0 foreign key (author_id) references authors
alter table author_work
add constraint author_workFK1 foreign key (work_id) references works

Java Skyline: Database

Java Skyline: Database
Muito util com database utilities e db explorers...
um mto bom eh o DbVisualizer http://www.minq.se/products/dbvis