Pages

November 13, 2016

DateTime JPA/Hibernate mapping

For mapping a Timestamp or Date in DB by hibernate we used joda time. Since, java 8 we use java.time package. But to success mapping with DateTime, LocalDate... type (joda time or java 8) we need some configurations.

1- Mapping joda time DateTime:

    With java 7 and earlier we use joda time. For mapping without any configuration we got an error. As solution, using jadira.

   
  <dependency>
      <groupId>org.jadira.usertype</groupId>
      <artifactId>usertype.jodatime</artifactId>
      <version>2.0.1</version>
  </dependency>

Example:
@Column
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private DateTime _date;

2- Mapping java 8 DateTime:

    With java 8 and hibernate 5 we need just a more dependency (hibernate-java8):
    - Maven:
   
  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>5.1.1.Final</version>
  </dependency>

- Gradle:

    compile group: 'org.hibernate', name: 'hibernate-java8', version: '5.1.1.Final'



August 18, 2014

Spring Data MongoDB

The Spring Data MongoDB project provides integration with the MongoDB document database.


The maven dependencies:

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-mongodb</artifactId>
            <version>1.5.2.RELEASE</version>
        </dependency>



Activating Spring Data repository support using Java Config:


import java.util.Arrays;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import com.mongodb.Mongo;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.WriteConcern;


@Configuration
@ComponentScan
@EnableMongoRepositories
public class Application extends AbstractMongoConfiguration{

    @Override
    
protected String getDatabaseName() {
        
return "tuto";
    }

    @Override
    
public Mongo mongo() throws Exception {
        ServerAddress serverAdress = 
new ServerAddress("localhost", 27017);        MongoCredential credential = MongoCredential.createMongoCRCredential("moez","tuto","secret".toCharArray());
        
//Do not use new Mongo(), is deprecated.
        Mongo mongo = new MongoClient(serverAdress,Arrays.asList(credential));
        mongo.setWriteConcern(WriteConcern.SAFE);
        
return mongo;
    }

  
    @Override
    @Bean(name=
"template")
    
public MongoTemplate mongoTemplate() throws Exception {
        
return new MongoTemplate(mongo(), getDatabaseName());
    }

}


Note: Do not use
new Mongo()
because is deprecated.

Here is a basic example of using the save operation and retrieving its contents.

import static org.springframework.data.mongodb.core.query.Criteria.where;
import static org.springframework.data.mongodb.core.query.Criteria.query;

…

Person p = new Person("Bob", 33);
mongoTemplate.insert(p);

Person qp = mongoTemplate.findOne(query(where("age").is(33)), Person.class);