2
respostas

Persistência de Dados com LocalTime

Olá,

Estava fazendo testes de persistência com a nova API do Java e ocorreu que ao persistir um atributo do tipo LocalTime no Mysql esse campo recebe uma hora a mais sobre seu valor.

Por exemplo o campo está com o valor 08:00:00 quando verifico no banco ele está com o valor 09:00:00, necessito fazer alguma configuração a mais no banco. Verifiquei o horário com a função NOW(), e o horário está certo.

Utilizo SpringBoot, Maven e Mysql

Ex Classe

@Entity
public class TimeCard {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(columnDefinition = "varchar(4)")
    private LocalTime morningsStart;

    @Column(columnDefinition = "varchar(4)")
    private LocalTime morningsEnd;

// getter and setters

Repository

@Transactional
public interface TimeCardRepository extends JpaRepository<TimeCard, Long> {
}

Classe de Teste

@RunWith(SpringRunner.class)
@SpringBootTest
public class TimeCardTest {

    @Autowired
    private TimeCardRepository timeCardRepository;

@Test
    public void persitenceTest() {

        TimeCard timeCard = new TimeCard();
        timeCard.setMorningsStart(LocalTime.of(8, 45));
        timeCard.setMorningsEnd(LocalTime.of(12, 00));

      timeCardRepository.save(timeCard);
}

Pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.com.rrodriguez</groupId>
    <artifactId>electronic-point</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>electronic-point</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.6</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-java8</artifactId>
            <version>5.1.0.Final</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
2 respostas

Opa, para ser sincero também não entendi a razão :(. Pode colocar o código no github para a gente dar uma olhada?

Olá Alberto,

Segue o código do projeto no github https://github.com/rrhodriguez/electronic-point.git