public class CompanyStruct implements Serializable {
public Integer comId; // PK
public String comName;
public String comDescription;
public Timestamp mutationDate;
}
public class EmployeeStruct implements Serializable {
public Integer empId; // PK
public Integer comId; // FK
public String empFirstName;
public String empLastName;
public Timestamp mutationDate;
}
public class CompanyBean implements EntityBean {
// 單一 create:傳入 Struct
public Integer ejbCreate(CompanyStruct struct) throws CreateException {
this.comId = struct.comId;
this.comName = struct.comName;
this.comDescription = struct.comDescription;
this.mutationDate = struct.mutationDate;
return null;
}
// 單一 getData:回傳 Struct
public CompanyStruct getData() {
CompanyStruct result = new CompanyStruct();
result.comId = this.comId;
result.comName = this.comName;
result.comDescription = this.comDescription;
result.mutationDate = this.mutationDate;
return result;
}
// 單一 setData:更新所有非 PK 欄位
public void setData(CompanyStruct struct) {
this.comName = struct.comName;
this.comDescription = struct.comDescription;
this.mutationDate = struct.mutationDate;
}
}
public Integer ejbCreate(CompanyStruct struct) throws CreateException {
this.comId = struct.comId; // PK 必須先設定
setData(struct); // 其餘欄位委派
return null;
}
@Entity
public class Company {
@Id Integer comId;
String comName;
String comDescription;
Timestamp mutationDate;
}
public class CompanyDTO { // 非實體,單純資料傳輸
Integer comId;
String comName;
String comDescription;
Timestamp mutationDate;
}
// Service 層
@Transactional
public CompanyDTO create(CompanyDTO dto) {
Company entity = mapper.toEntity(dto);
entity = repo.save(entity);
return mapper.toDto(entity);
}
模式二確立「Value Object 封裝多欄位、單一方法傳輸」核心思想,解決分散式細粒度介面的效能與一致性問題。此模式演變為現代 DTO (Data Transfer Object) 模式,廣泛應用於微服務、REST API、GraphQL 等架構中。