How to make a basic configuration in the persistence.xml file to access a database in SQL Server 2008?

Asked

Viewed 819 times

1

I searched about how to set up the file persistence.xml in a basic way, however, I was even more confused about this configuration.

So I would like to know how I can make a basic configuration in the file persistence.xml in accordance with the JPA and the Hibernate to access a database on SQL Server 2008?


Information relating to the database:

My Connection String:

"Data Source=CARVALHO-PC\\LOGIXMINESYSTEM;Initial Catalog=TarefaExemplo;Persist Security Info=True;User ID=sa;Password=minhasenha"

The bank has a single table called tarefas with the fields id int, descricao VARCHAR(50), finalizado int and data_finalizado DATETIME.

Project and application structure information:

I’ve added all the libraries (Jars) for Hibernate and JPA and also the JDBC driver for SQL Server.

And the structure of the project is as follows:

...\EXEMPLOHIBERNATE\SRC
├───exemplohibernate
├───META-INF
└───model

Since within the model package there is a single class, class Tarefa:

package model;

import java.util.Calendar;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
/**
 * @author Dener
 */

@Entity
@Table(name="tarefas")
public class Tarefa {
    
    @GeneratedValue    
    @Id
    private Long id;
    
    private String descricao;
    private boolean finalizado;
    
    @Column(name = "data_finalizado", nullable = false)
    @Temporal(TemporalType.DATE)
    private Calendar dataFinalizacao;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public boolean isFinalizado() {
        return finalizado;
    }

    public void setFinalizado(boolean finalizado) {
        this.finalizado = finalizado;
    }

    public Calendar getDataFinalizacao() {
        return dataFinalizacao;
    }

    public void setDataFinalizacao(Calendar dataFinalizacao) {
        this.dataFinalizacao = dataFinalizacao;
    }
    
}

This is the class that has the annotations for the Framework. And the other packages do not contain classes yet.

Note:

The sample application is for the desktop platform and the IDE I’m using and Netbeans.

  • I have a persistence template q made to test in javadb or mysql, will it serve? To no SQL server here to test.

  • @Diegof I already managed to solve the problem, I used a Netbeans wizard to create the file.

1 answer

1

You can try it:

 <?xml version="1.0" encoding="UTF-8"?>
    <persistence-unit name="tarefas">

     <provider>org.hibernate.ejb.HibernatePersistence</provider>

     <class>model.Tarefa</class>

           <properties>
               <property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
               <property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost:1433;databaseName=TarefaExemplo"></property>
               <property name="javax.persistence.jdbc.user" value="sa"></property>
               <property name="javax.persistence.jdbc.password" value="minhaSenha"></property>
           </properties>
       </persistence-unit>
    </persistence>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.