How can I annotate a filed of type DateTime (joda) in scala using jpa?

379 views
Skip to first unread message

Matthias Heininger

unread,
Dec 19, 2013, 10:22:46 AM12/19/13
to scala...@googlegroups.com
Hi,

How can I annotate a filed of type DateTime (joda) in scala using jpa?

Actually my model looks like this..

package models  
  
import javax.persistence._
import scala.reflect._
import com.avaje.ebean._
import com.avaje.ebean.annotation._
import org.joda.time.DateTime
   
@Entity
@Table(name="contact")  
class Company {  
  
  @Id
  @GeneratedValue
  val id:Long = 0
  
  @OneToOne
  val client: Client = null
  
  @(I DONT KNOW WHAT TO PUT HERE, CANT FIND A SOLUTION ON GOOGLE)
  val founding: DateTime = null
      
    
}

Raphael Schweizer

unread,
Dec 19, 2013, 2:05:49 PM12/19/13
to scala...@googlegroups.com
Using an @Converter class for [DateTime, String] (String is AFAIK the simplest way to preserve all information (TZ, ...) in human readable format - I like to be able to look at my DB with whatever tool).

Andreas Joseph Krogh

unread,
Dec 19, 2013, 2:37:50 PM12/19/13
to scala...@googlegroups.com
You don't need any explicit annotations in JPA-2.1, just the converters, with autoApply=true:
 
@Converter(autoApply = true)
class JodaDateTimeConverter extends AttributeConverter[DateTime, Timestamp] {
    def convertToDatabaseColumn(attribute: DateTime): Timestamp = {
        if (attribute == null) {
            null
        } else {
            new Timestamp(attribute.getMillis)
        }
    }
    def convertToEntityAttribute(dbData: Timestamp): DateTime = {
        if (dbData == null) {
            null
        }
        else {
            new DateTime(dbData.getTime)
        }
    }
}
 
@Converter(autoApply = true)
class OptionDateTimeConverter extends AttributeConverter[Option[DateTime], Timestamp] {
    def convertToDatabaseColumn(attribute: Option[DateTime]): Timestamp = {
        attribute.map(v => new Timestamp(v.getMillis)).orNull
    }
    def convertToEntityAttribute(dbData: Timestamp): Option[DateTime] = {
        Option(dbData).map(v => new DateTime(v.getTime))
    }
}
 
 
 
--
Andreas Joseph Krogh <and...@officenet.no>      mob: +47 909 56 963
Senior Software Developer / CTO - OfficeNet AS - http://www.officenet.no
Public key: http://home.officenet.no/~andreak/public_key.asc
 

Andreas Joseph Krogh

unread,
Dec 19, 2013, 2:44:02 PM12/19/13
to scala...@googlegroups.com
If you use Spring to wire up the deps (and free you completely from usage of persistence.xml), you can use a custom persistence-unit-post-processor to scan for your converters and register them, like this:
<bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="packagesToScan" value="com.mycompany.api"/>
        <property name="persistenceUnitPostProcessors" ref="puPosProcessor"/>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>
        <property name="jpaDialect" ref="jpaDialect"/>
</bean>

<bean id="puPosProcessor" class="com.mycompany.ClasspathScanningPersistenceUnitPostProcessor">
        <constructor-arg value="com.mycompany.api"/>
</bean>
class ClasspathScanningPersistenceUnitPostProcessor(basePackage: String) extends PersistenceUnitPostProcessor with Loggable {
        def postProcessPersistenceUnitInfo(pui: MutablePersistenceUnitInfo) {
                val provider = new ClassPathScanningCandidateComponentProvider(false)
                provider.addIncludeFilter(new AnnotationTypeFilter(classOf[Converter]))
                import scala.collection.JavaConversions._
                for (definition <- provider.findCandidateComponents(basePackage)) {
                        debug(s"Registering classpath-scanned entity ${definition.getBeanClassName} in persistence unit info!")
                        pui.addManagedClassName(definition.getBeanClassName)
                }
        }
}

Matthias Heininger

unread,
Dec 19, 2013, 3:32:25 PM12/19/13
to scala...@googlegroups.com
Thanks for your reply..

The Converter idea didnt work out that well. Can you tell me in what package I can find this exact Converter?
So I can use this method within my model class?

Would you recommend using spring libs in a play project?


Andreas Joseph Krogh

unread,
Dec 19, 2013, 3:37:42 PM12/19/13
to scala...@googlegroups.com
Converter is in javax.persistence.Converter
 
It's new in JPA-2.1 (EclipseLink >= 2.5 and Hibernate >= 4.3)
 
Yes, there's nothing wrong with using Spring in a Play-project.

Matthias Heininger

unread,
Dec 19, 2013, 3:54:35 PM12/19/13
to scala...@googlegroups.com
Thank you kindly.. 
it was my old persistence api..

I`m totally new to scala so I`m not totally getting it how to use these methods inside my "company" model!? Can you point my to the right direction?

Thanks again

Andreas Joseph Krogh

unread,
Dec 19, 2013, 3:58:06 PM12/19/13
to scala...@googlegroups.com
Just as you would with JAVA.

Andreas Joseph Krogh

unread,
Dec 19, 2013, 3:59:08 PM12/19/13
to scala...@googlegroups.com
På torsdag 19. desember 2013 kl. 21:58:06, skrev Andreas Joseph Krogh <and...@officenet.no>:
På torsdag 19. desember 2013 kl. 21:54:35, skrev Matthias Heininger <muenc...@googlemail.com>:
Thank you kindly.. 
it was my old persistence api..
 
I`m totally new to scala so I`m not totally getting it how to use these methods inside my "company" model!? Can you point my to the right direction?
 
Thanks again
 
Just as you would with JAVA.
 
Oh, and don't use val's, as JPA-objects are muteable, use var's.

Matthias Heininger

unread,
Dec 19, 2013, 4:06:40 PM12/19/13
to scala...@googlegroups.com
thank you.. 

Matthias Heininger

unread,
Dec 19, 2013, 4:19:41 PM12/19/13
to scala...@googlegroups.com
Ok.. actually I`m really trying hard to manage this but it doesnt seem to work..

Could you please provide me an example? This would be great..

Andreas Joseph Krogh

unread,
Dec 19, 2013, 4:24:16 PM12/19/13
to scala...@googlegroups.com
På torsdag 19. desember 2013 kl. 22:19:41, skrev Matthias Heininger <muenc...@googlemail.com>:
Ok.. actually I`m really trying hard to manage this but it doesnt seem to work..
 
Could you please provide me an example? This would be great..
 
You stll haven't told us what's not working and what you tried?
 
Anyway - here's an example, mixing Scala/JPA/Spring/EclipseLink: https://github.com/andreak/on-example-rpm
 
It uses, among other things, this converter:
Reply all
Reply to author
Forward
0 new messages