Estou estudando os padroes de persistencia e ainda não estou apto a resolver os meus próprios erros de código.
Se alguém com um pouquinho de paciencia puder dar uma olhadinha eu agradeço.
package Control;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
*
* @author RICARDO
*/
@Entity
@Table(name = "funcoes")
@NamedQueries({
@NamedQuery(name = "Funcoes.findAll", query = "SELECT f FROM Funcoes f"),
@NamedQuery(name = "Funcoes.findById", query = "SELECT f FROM Funcoes f WHERE f.id = :id"),
@NamedQuery(name = "Funcoes.findByFuncao", query = "SELECT f FROM Funcoes f WHERE f.funcao = :funcao"),
@NamedQuery(name = "Funcoes.findByData", query = "SELECT f FROM Funcoes f WHERE f.data = :data"),
@NamedQuery(name = "Funcoes.findByHora", query = "SELECT f FROM Funcoes f WHERE f.hora = :hora"),
@NamedQuery(name = "Funcoes.findByFuncionario", query = "SELECT f FROM Funcoes f WHERE f.funcionario = :funcionario")
})
public class Funcoes implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "FUNCAO", length = 255)
private String funcao;
@Column(name = "DATA")
@Temporal(TemporalType.DATE)
private Date dt;
@Column(name = "HORA")
@Temporal(TemporalType.TIME)
private Date hora;
@Column(name = "FUNCIONARIO")
private Integer funcionario;
public Funcoes() {
}
public Funcoes(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFuncao() {
return funcao;
}
public void setFuncao(String funcao) {
this.funcao = funcao;
}
public Date getData() {
return dt;
}
public void setData(Date dt) {
this.dt = dt;
}
public Date getHora() {
return hora;
}
public void setHora(Date hora) {
this.hora = hora;
}
public Integer getFuncionario() {
return funcionario;
}
public void setFuncionario(Integer funcionario) {
this.funcionario = funcionario;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Funcoes)) {
return false;
}
Funcoes other = (Funcoes) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "Control.Funcoes[id=" + id + "]";
}
}
// a seguir um teste para leitura no shell e arquivamento
mas está dando uma exception no hibernate
package Control;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class Registro {
public static int count = 0;
public static void main(String args[]) throws FileNotFoundException, ParseException{
Funcoes funcoes = new Funcoes();
SimpleDateFormat data = new SimpleDateFormat("dd/mm/yyyy");
SimpleDateFormat hora = new SimpleDateFormat("hh:mm:ss");
Scanner scan = new Scanner(new File("C:\\Users\\RICARDO\\Documents\\Henry\\MeusArquivosSaida\\Funções.txt"));
while(scan.hasNextLine()) {
String linha = scan.nextLine();
String[] campos = linha.split(" ");
String funcao = campos[0];
funcoes.setFuncao(funcao);
Date dt = data.parse(campos[1]);
funcoes.setData(dt);
Date hr = hora.parse(campos[2]);
funcoes.setHora(hr);
int funcionario = Integer.parseInt(campos[3]);
funcoes.setFuncionario(funcionario);
//Seu processamento aqui.
EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaApplication3PU");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(funcoes);
tx.commit();
em.close();
emf.close();
String str = "";
for(int x = 0; x < campos.length; x++){
str += campos[x] + "\t";
}
System.out.println(str);
count++;
}
}
}
// a unidade de persistencia
<?xml version="1.0" encoding="UTF-8" ?>
- <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
- <persistence-unit name="JavaApplication3PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Control.Funcoes</class>
- <properties>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.password" value="1234" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/ponto" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
Valeu irmãos!!!