Estou fazendo alguns testes, sei que para fazer o SELECT usando o Hibernate não preciso usar SQL nativo, mas quero fazer assim para alguns testes.
Faço um SELECT nativo desta forma:
Query query = entityManager.createNativeQuery("SELECT codigo, nome, endereco, tel1, tel2 FROM clientes WHERE nome LIKE 'A%'", ClienteMOD.class);
query.unwrap(SQLQueryImpl.class).setResultTransformer(new AliasToBeanResultTransformer(ClienteMOD.class));
return (List<ClienteMOD>) query.getResultList();
Minha classe ClienteMOD:
@Entity
@Table(name = "clientes")
public class ClienteMOD {
public ClienteMOD() {
}
public ClienteMOD(int id, String nome, String endereco) {
this.id = id;
this.nome = nome;
this.endereco = endereco;
}
public ClienteMOD(int id, String nome, String endereco, String telefone1, String telefone2) {
this.id = id;
this.nome = nome;
this.endereco = endereco;
this.telefone1 = telefone1;
this.telefone2 = telefone2;
}
@Id
@Column(name = "codigo")
private Integer id;
@Column(name = "nome")
private String nome;
@Column(name = "endereco")
private String endereco;
@Column(name = "tel1")
private String telefone1;
@Column(name = "tel2")
private String telefone2;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getTelefone1() {
return telefone1;
}
public void setTelefone1(String telefone1) {
this.telefone1 = telefone1;
}
public String getTelefone2() {
return telefone2;
}
public void setTelefone2(String telefone2) {
this.telefone2 = telefone2;
}
}
Quando executo este SELECT, ocorre este erro:
javax.persistence.PersistenceException: org.hibernate.PropertyNotFoundException: Could not find setter for alias1 on class modelos.ClienteMOD
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1361)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1289)
at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:261)
at dao.ClientesDAO.getByNome(ClientesDAO.java:44)
at forms.FormPesqClientes.pesquisarClientes(FormPesqClientes.java:154)
at forms.FormPesqClientes.tfConteudoPesquisaKeyPressed(FormPesqClientes.java:132)
at forms.FormPesqClientes.access$000(FormPesqClientes.java:10)
at forms.FormPesqClientes$1.keyPressed(FormPesqClientes.java:63)
at java.awt.Component.processKeyEvent(Component.java:6483)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2830)
at java.awt.Component.processEvent(Component.java:6302)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
at java.awt.Component.dispatchEventImpl(Component.java:4752)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:724)
at java.awt.EventQueue$4.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:109)
at java.awt.WaitDispatchSupport$2.run(WaitDispatchSupport.java:184)
at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:229)
at java.awt.WaitDispatchSupport$4.run(WaitDispatchSupport.java:227)
at java.security.AccessController.doPrivileged(Native Method)...
O que estou fazendo de errado?