2013-11-03 103 views
0

我正在使用Hibernate創建模型類,但我不知道如何處理這種關係。使用同一個實體將這種關係映射到2個實體

我有三張桌子。

地址,員工和人員。

一個高手可以有一個地址,一個人可以有一個地址。

我不知道如何映射。

因爲我認爲使用嵌入註釋,但沒有工作。

首先是映射我的類,我需要把這兩個實體放在地址類中?

我需要使用哪種註釋?

我用id屬性的超類和每個類擴展。

I'm用mysql

我個人類

@Entity 
@Table(name = "destinatario") 
public class Destinatario extends Persistent { 

     private static final long serialVersionUID = -7091318100871934315L; 

     @ManyToOne(cascade = CascadeType.PERSIST) 
     @JoinColumn(name = "endereco_id", referencedColumnName = "id") 
     private Endereco endereco; 

     @NotNull 
     @Size(max = 60) 
     @Column(name = "razao_social") 
     private String razaoSocial; 

     @NotNull 
     @Size(max = 14) 
     @Column(name = "inscricao_estadual") 
     private String inscricaoEstadual; 

     @Size(max = 9) 
     @Column(name = "inscricao_suframa") 
     private String inscricaoSuframa; 

     @Size(max = 60) 
     @Column(name = "email") 
     private String email; 

     @Size(max = 14) 
     @Column(name = "cnpj") 
     private String cnpj; 

     @Size(max = 11) 
     @Column(name = "cpf") 
     private String cpf; 

     @OneToMany 
     @JoinColumn(name = "destinatario_id") 
     private List<NotaFiscal> notaFiscais; 
} 

我的地址類

@Entity 
@Table(name = "endereco") 
public class Endereco extends Persistent { 

     private static final long serialVersionUID = -3308931308130690090L; 

     public enum UF { 

       AC("AC", "Acre"), 
       AL("AL", "Alagoas"), 
       AP("AP", "Amapá"), 
       AM("AM", "Amazonas"), 
       BA("BA", "Bahia"), 
       CE("CE", "Ceara"), 
       DF("DF", "Distrito Federal"), 
       ES("ES", "Espirito Santo"), 
       GO("GO", "Goiás"), 
       MA("MA", "Maranhão"), 
       MT("MT", "Mato Grosso"), 
       MS("MS", "Mato Grosso do Sul"), 
       MG("MG", "Minas Gerais"), 
       PA("PA", "Pará"), 
       PB("PB", "Paraíba"), 
       PR("PR", "Paraná"), 
       PE("PE", "Pernambuco"), 
       PI("PI", "Piauí"), 
       RJ("RJ", "Rio de Janeiro"), 
       RN("RN", "Rio Grande do Norte"), 
       RS("RS", "Rio Grande do Sul"), 
       RO("RO", "Rondônia"), 
       RR("RR", "Roraima"), 
       SC("SC", "Santa Catarina"), 
       SP("SP", "São Paulo"), 
       SE("SE", "Sergipe"), 
       TO("TO", "Tocantins"); 

       private final String index; 
       private String descricao; 

       private UF(String index, String descricao) { 
         this.index = index; 
         this.descricao = descricao; 
       } 

       public String getNomeEstado() { 
         return descricao; 
       } 

       public String getIndex() { 
         return index; 
       } 

     } 

     @NotNull 
     @Size(max = 60) 
     @Column(name = "logradouro", unique = true) 
     private String logradouro; 

     @NotNull 
     @Size(max = 60) 
     @Column(name = "numero", unique = true) 
     private String numero; 

     @Size(max = 60) 
     @Column(name = "complemento") 
     private String complemento; 

     @NotNull 
     @Size(max = 60) 
     @Column(name = "bairro", unique = true) 
     private String bairro; 

     @NotNull 
     @Size(max = 60) 
     @Column(name = "municipio", unique = true) 
     private String municipio; 

     @Enumerated(EnumType.STRING) 
     @NotNull 
     //@Type(type = UFType.TYPE) 
     @Column(name = "uf", columnDefinition = "varchar", length = 2) 
     private UF uf; 

     @NotNull 
     @Size(max = 8) 
     @Column(name = "cep", unique = true) 
     private String cep; 

     @Size(max = 14) 
     @Column(name = "telefone") 
     private String telefone; 
} 

我的方法來運行,並通過XML源創建人

public static void main(String[] args) { 

     new Processadora().extrairDadosXml("diego"); 

     ArquivoNotaFiscal arquivoNotaFiscal = null; 
     Destinatario destinatario = null; 
     NotaFiscal notaFiscal = null; 

     destinatario = createDestinatario(); 
     arquivoNotaFiscal = createArquivoNotaFiscal(); 
     notaFiscal = createNotaFiscal(arquivoNotaFiscal, emitente, destinatario); 

     destinatario.setNotaFiscais(Arrays.asList(notaFiscal)); 

     DestinatarioDAO<Destinatario> destinatarioDAO = new DestinatarioDAOImpl<>(); 

     Session session = HibernateSessionFactory.getSession(); 
     Transaction transaction = session.getTransaction(); 
     transaction.begin(); 

     destinatarioDAO.save(destinatario); 

     transaction.commit(); 
} 

private static Destinatario createDestinatario() { 

     Destinatario destinatario = new Destinatario(); 

     Endereco endereco = new Endereco(); 

     endereco.setLogradouro(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getLogradouro()); 
     endereco.setNumero(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getNumero()); 
     endereco.setBairro(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getBairro()); 
     endereco.setComplemento(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getComplemento()); 
     endereco.setCep(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getCep()); 
     endereco.setMunicipio(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getMunicipio()); 
     endereco.setUf(UF.valueOf(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getUF())); 
     endereco.setTelefone(nFeProc.getNfe().getInfNFe().getDestinatario().getEndereco().getTelefone()); 

     destinatario.setEndereco(endereco); 

     destinatario.setRazaoSocial(nFeProc.getNfe().getInfNFe().getDestinatario().getRazaoSocial()); 
     destinatario.setInscricaoEstadual(nFeProc.getNfe().getInfNFe().getDestinatario().getInscricaoEstadual()); 
     destinatario.setInscricaoSuframa(nFeProc.getNfe().getInfNFe().getDestinatario().getInscricaoSuframa()); 
     destinatario.setEmail(nFeProc.getNfe().getInfNFe().getDestinatario().getEmail()); 
     destinatario.setCnpj(nFeProc.getNfe().getInfNFe().getDestinatario().getCnpj()); 
     destinatario.setCpf(nFeProc.getNfe().getInfNFe().getDestinatario().getCpf()); 

     return destinatario; 

} 

我的數據庫hav e外鍵約束,我使用mysql

+0

這一切都取決於表連接在一起的方式。你有地址內的外鍵,還是內部的員工和人員? –

+0

你可以閱讀一些以前的文章,以獲得更多的想法:[article1](http://stackoverflow.com/questions/38057/why-onetomany-does-not-work-with-inheritance-in-hibernate)[article2]( http://stackoverflow.com/questions/14134695/bi-directional-one-to-many-with-inheritance-not-working-jpa-with-hibernate-3-5)。另請參閱hibernate文檔[這裏](http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/inheritance.html) –

+0

我在表人員和僱員的外鍵 –

回答

0

我發現我的問題,我只保存一個對象,因爲我認爲如果我使用保存包含其他的對象將保存,但我需要保存地址和以後的人。

所以這種方式everthing工作。

相關問題