Metodos Getters Setters Dinâmicos

Forum destinado a Linguagem JAVA! Classes, orientação a objeto, conexão com banco, chamada de procedures, etc
Responder
leandro_bonato
Rank: Programador Júnior
Rank: Programador Júnior
Mensagens: 22
Registrado em: Seg, 14 Out 2013 9:24 pm

Boa noite Srs., estou com querendo fazer um desenvolvimento dos métodos Getters e Setters dinamicamente, isto inclui os atributos de serem dinâmicos também, segue o código exemplo dos atributos e métodos Getters e Setters fixos, sem serem dinâmicos. Há alguma forma de desenvolver isto dinamicamente? Para avisar preciso utilizar eles em banco de dados. Obrigado!

Selecionar tudo

package beans;
import java.beans.*;
import java.sql.Date;

public class ClassesBeans{
     
private int codigo;
private String nome;

public int getCodigo(){
return codigo;
}

public String getNome(){
return nome;
}

public void setCodigo(înt codigo){
this.codigo = codigo;
}

public void setNome(){
this.nome = nome;
}
}


Este é apenas um exemplo de toda a Classes, o resto é praticamente igual.

Deverá ser feito todas essas linhas de código dinamicamente, que eu não precise fazer isso fixo e depois tenha que buscar esses campos fixos, é só eu apontar a classe e o método e ele constrói o que eu preciso. A ideia é basicamente isso. Obrigado!
leandro_bonato
Rank: Programador Júnior
Rank: Programador Júnior
Mensagens: 22
Registrado em: Seg, 14 Out 2013 9:24 pm

Ninguém ???? :shock:
Avatar do usuário
dr_gori
Moderador
Moderador
Mensagens: 5023
Registrado em: Seg, 03 Mai 2004 3:08 pm
Localização: Portland, OR USA
Contato:
Thomas F. G

Você já respondeu a dúvida de alguém hoje?
https://glufke.net/oracle/search.php?search_id=unanswered

Deixa eu entender melhor: Você quer gerar automático os get e sets conforme os campos de uma tabela ?
Acredito que se for isso que você quer, basta fazer um CURSOR na ALL_TAB_COLUMNS do Oracle.

Assim:

Selecionar tudo

begin
  DBMS_OUTPUT.PUT_LINE('package beans;');
  DBMS_OUTPUT.PUT_LINE('import java.beans.*;');
  DBMS_OUTPUT.PUT_LINE('import java.sql.Date;');
  DBMS_OUTPUT.PUT_LINE('');
  DBMS_OUTPUT.PUT_LINE('public class ClassesBeans{');
  DBMS_OUTPUT.PUT_LINE('');
  
  FOR vcur IN (select * from all_tab_columns WHERE table_name='FND_USER')
  LOOP
    -- Aqui esta INT pra todos, você vai ter que fazer um DE-PARA, tipo, se for varchar
    -- converte pra String, se for Number, INT ou float, etc.
    DBMS_OUTPUT.PUT_LINE('private int '|| LOWER(vcur.column_name)||';');
  END LOOP;

  DBMS_OUTPUT.PUT_LINE('');

  FOR vcur IN (select * from all_tab_columns WHERE table_name='FND_USER')
  LOOP
    -- Aqui esta INT pra todos, você vai ter que fazer um DE-PARA, tipo, se for varchar
    -- converte pra String, se for Number, INT ou float, etc.
    DBMS_OUTPUT.PUT_LINE('public int get'||INITCAP(vcur.column_name)||'(){');
    DBMS_OUTPUT.PUT_LINE('return '||LOWER(vcur.column_name)||';');
    DBMS_OUTPUT.PUT_LINE('}');
    DBMS_OUTPUT.PUT_LINE('');

    DBMS_OUTPUT.PUT_LINE('public int set'||INITCAP(vcur.column_name)||'(int '||LOWER(vcur.column_name)||'){');
    DBMS_OUTPUT.PUT_LINE('this.'||LOWER(vcur.column_name)||'='||LOWER(vcur.column_name)||';');
    DBMS_OUTPUT.PUT_LINE('}');
    DBMS_OUTPUT.PUT_LINE('');    

    DBMS_OUTPUT.PUT_LINE('public void set'||INITCAP(vcur.column_name)||'()');
    DBMS_OUTPUT.PUT_LINE('this.'||LOWER(vcur.column_name)||'='||LOWER(vcur.column_name)||';');
    DBMS_OUTPUT.PUT_LINE('}');
    DBMS_OUTPUT.PUT_LINE('');    
  END LOOP;
end;  
No exemplo acima, ele gera o seguinte texto:

Selecionar tudo

package beans;
import java.beans.*;
import java.sql.Date;
 
public class ClassesBeans{
 
private int password_accesses_left;
private int gcn_code_combination_id;
private int person_party_id;
private int user_guid;
private int user_id;
private int user_name;
private int last_update_date;
private int last_updated_by;
private int creation_date;
private int created_by;
private int last_update_login;
private int encrypted_foundation_password;
private int encrypted_user_password;
private int session_number;
private int start_date;
private int end_date;
private int description;
private int last_logon_date;
private int password_date;
private int password_lifespan_accesses;
private int password_lifespan_days;
private int employee_id;
private int email_address;
private int fax;
private int customer_id;
private int supplier_id;
private int web_password;
 
public int getPassword_Accesses_Left(){
return password_accesses_left;
}
 
public int setPassword_Accesses_Left(int password_accesses_left){
this.password_accesses_left=password_accesses_left;
}
 
public void setPassword_Accesses_Left()
this.password_accesses_left=password_accesses_left;
}
 
public int getGcn_Code_Combination_Id(){
return gcn_code_combination_id;
}
 
public int setGcn_Code_Combination_Id(int gcn_code_combination_id){
this.gcn_code_combination_id=gcn_code_combination_id;
}
 
public void setGcn_Code_Combination_Id()
this.gcn_code_combination_id=gcn_code_combination_id;
}
 
public int getPerson_Party_Id(){
return person_party_id;
}
 
public int setPerson_Party_Id(int person_party_id){
this.person_party_id=person_party_id;
}
 
public void setPerson_Party_Id()
this.person_party_id=person_party_id;
}
 
public int getUser_Guid(){
return user_guid;
}
 
public int setUser_Guid(int user_guid){
this.user_guid=user_guid;
}
 
public void setUser_Guid()
this.user_guid=user_guid;
}
 
public int getUser_Id(){
return user_id;
}
 
public int setUser_Id(int user_id){
this.user_id=user_id;
}
 
public void setUser_Id()
this.user_id=user_id;
}
 
public int getUser_Name(){
return user_name;
}
 
public int setUser_Name(int user_name){
this.user_name=user_name;
}
 
public void setUser_Name()
this.user_name=user_name;
}
 
public int getLast_Update_Date(){
return last_update_date;
}
 
public int setLast_Update_Date(int last_update_date){
this.last_update_date=last_update_date;
}
 
public void setLast_Update_Date()
this.last_update_date=last_update_date;
}
 
public int getLast_Updated_By(){
return last_updated_by;
}
 
public int setLast_Updated_By(int last_updated_by){
this.last_updated_by=last_updated_by;
}
 
public void setLast_Updated_By()
this.last_updated_by=last_updated_by;
}
 
public int getCreation_Date(){
return creation_date;
}
 
public int setCreation_Date(int creation_date){
this.creation_date=creation_date;
}
 
public void setCreation_Date()
this.creation_date=creation_date;
}
 
public int getCreated_By(){
return created_by;
}
 
public int setCreated_By(int created_by){
this.created_by=created_by;
}
 
public void setCreated_By()
this.created_by=created_by;
}
 
public int getLast_Update_Login(){
return last_update_login;
}
 
public int setLast_Update_Login(int last_update_login){
this.last_update_login=last_update_login;
}
 
public void setLast_Update_Login()
this.last_update_login=last_update_login;
}
 
public int getEncrypted_Foundation_Password(){
return encrypted_foundation_password;
}
 
public int setEncrypted_Foundation_Password(int encrypted_foundation_password){
this.encrypted_foundation_password=encrypted_foundation_password;
}
 
public void setEncrypted_Foundation_Password()
this.encrypted_foundation_password=encrypted_foundation_password;
}
 
public int getEncrypted_User_Password(){
return encrypted_user_password;
}
 
public int setEncrypted_User_Password(int encrypted_user_password){
this.encrypted_user_password=encrypted_user_password;
}
 
public void setEncrypted_User_Password()
this.encrypted_user_password=encrypted_user_password;
}
 
public int getSession_Number(){
return session_number;
}
 
public int setSession_Number(int session_number){
this.session_number=session_number;
}
 
public void setSession_Number()
this.session_number=session_number;
}
 
public int getStart_Date(){
return start_date;
}
 
public int setStart_Date(int start_date){
this.start_date=start_date;
}
 
public void setStart_Date()
this.start_date=start_date;
}
 
public int getEnd_Date(){
return end_date;
}
 
public int setEnd_Date(int end_date){
this.end_date=end_date;
}
 
public void setEnd_Date()
this.end_date=end_date;
}
 
public int getDescription(){
return description;
}
 
public int setDescription(int description){
this.description=description;
}
 
public void setDescription()
this.description=description;
}
 
public int getLast_Logon_Date(){
return last_logon_date;
}
 
public int setLast_Logon_Date(int last_logon_date){
this.last_logon_date=last_logon_date;
}
 
public void setLast_Logon_Date()
this.last_logon_date=last_logon_date;
}
 
public int getPassword_Date(){
return password_date;
}
 
public int setPassword_Date(int password_date){
this.password_date=password_date;
}
 
public void setPassword_Date()
this.password_date=password_date;
}
 
public int getPassword_Lifespan_Accesses(){
return password_lifespan_accesses;
}
 
public int setPassword_Lifespan_Accesses(int password_lifespan_accesses){
this.password_lifespan_accesses=password_lifespan_accesses;
}
 
public void setPassword_Lifespan_Accesses()
this.password_lifespan_accesses=password_lifespan_accesses;
}
 
public int getPassword_Lifespan_Days(){
return password_lifespan_days;
}
 
public int setPassword_Lifespan_Days(int password_lifespan_days){
this.password_lifespan_days=password_lifespan_days;
}
 
public void setPassword_Lifespan_Days()
this.password_lifespan_days=password_lifespan_days;
}
 
public int getEmployee_Id(){
return employee_id;
}
 
public int setEmployee_Id(int employee_id){
this.employee_id=employee_id;
}
 
public void setEmployee_Id()
this.employee_id=employee_id;
}
 
public int getEmail_Address(){
return email_address;
}
 
public int setEmail_Address(int email_address){
this.email_address=email_address;
}
 
public void setEmail_Address()
this.email_address=email_address;
}
 
public int getFax(){
return fax;
}
 
public int setFax(int fax){
this.fax=fax;
}
 
public void setFax()
this.fax=fax;
}
 
public int getCustomer_Id(){
return customer_id;
}
 
public int setCustomer_Id(int customer_id){
this.customer_id=customer_id;
}
 
public void setCustomer_Id()
this.customer_id=customer_id;
}
 
public int getSupplier_Id(){
return supplier_id;
}
 
public int setSupplier_Id(int supplier_id){
this.supplier_id=supplier_id;
}
 
public void setSupplier_Id()
this.supplier_id=supplier_id;
}
 
public int getWeb_Password(){
return web_password;
}
 
public int setWeb_Password(int web_password){
this.web_password=web_password;
}
 
public void setWeb_Password()
this.web_password=web_password;
}
leandro_bonato
Rank: Programador Júnior
Rank: Programador Júnior
Mensagens: 22
Registrado em: Seg, 14 Out 2013 9:24 pm

Obrigado pela resposta. Certo como eu faria para aplicar isso em tempo de execução no Java? Por que ai ele está em um bloco anônimo. Por que isto será utilizado para Selects, Inserts, Updates e Deletes com SQL Dinâmico no Oracle.

Obrigado!
Avatar do usuário
dr_gori
Moderador
Moderador
Mensagens: 5023
Registrado em: Seg, 03 Mai 2004 3:08 pm
Localização: Portland, OR USA
Contato:
Thomas F. G

Você já respondeu a dúvida de alguém hoje?
https://glufke.net/oracle/search.php?search_id=unanswered

Eu não sei se eu to entendendo direito o que você quer fazer.
No caso, esses códigos foram gerados no ORACLE.

Você quer criar uma JAVA stored procedure no banco com esses codigos ?
Ou quer usar isso em um programa java externo ?
leandro_bonato
Rank: Programador Júnior
Rank: Programador Júnior
Mensagens: 22
Registrado em: Seg, 14 Out 2013 9:24 pm

Boa tarde, obrigado pela resposta. Eu gostaria de usá-los no Java Externo, porém já que você tocou no assunto, se você puder me mostrar das duas formas para que eu veja qual se encaixa melhor no que eu quero eu agradeceria. Se for um pouco inconveniente demais pode ser apenas o modelo do Java Externo apenas. Obrigado!
Responder
  • Informação
  • Quem está online

    Usuários navegando neste fórum: Nenhum usuário registrado e 1 visitante