--- /dev/null
+package de.spring.example.persistence.converters;
+
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
+import java.time.ZoneId;
+import java.time.ZonedDateTime;
+
+import javax.persistence.AttributeConverter;
+import javax.persistence.Converter;
+
+@Converter(autoApply = true)
+public class OffsetDateTimeAttributeConverter implements AttributeConverter<OffsetDateTime, Timestamp> {
+
+ @Override
+ public Timestamp convertToDatabaseColumn(OffsetDateTime offsetDateTime) {
+ Timestamp timestamp = null;
+
+ if (offsetDateTime != null) {
+ timestamp = Timestamp.valueOf(offsetDateTime.toLocalDateTime());
+ }
+
+ return timestamp;
+ }
+
+ @Override
+ public OffsetDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
+ OffsetDateTime offsetDateTime = null;
+
+ if (sqlTimestamp != null) {
+ final LocalDateTime localDateTime = sqlTimestamp.toLocalDateTime();
+ final ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
+ offsetDateTime = zonedDateTime.toOffsetDateTime();
+ }
+
+ return offsetDateTime;
+ }
+
+}
--- /dev/null
+package de.spring.example.persistence.domain;
+
+import java.io.Serializable;
+import java.time.OffsetDateTime;
+import java.util.Set;
+
+import javax.persistence.Column;
+import javax.persistence.Convert;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+//import javax.persistence.NamedNativeQueries;
+//import javax.persistence.NamedNativeQuery;
+//import javax.persistence.NamedQueries;
+//import javax.persistence.NamedQuery;
+import javax.persistence.OneToMany;
+import javax.persistence.Table;
+import javax.validation.constraints.Max;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+
+import de.spring.example.persistence.converters.OffsetDateTimeAttributeConverter;
+
+@Entity
+@Table(name="ad", schema="mybatis_example")
+// 1. Named query is JPL. It is portable.
+// 2. Instead of annotating the domain class we should be using @Query annotation at the query method
+// because it should be cleaner :)
+// So you'd better use @Query.
+//http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query
+//See: de.spring.persistence.example.repository.AdRepository
+//@NamedQueries(
+// {
+// @NamedQuery(
+// name="Ad.findByIdQuery",
+// query="select a from Ad a where a.id = :id)
+// }
+//
+//)
+// 1. Native query IS NOT JPL. It is not portable and it is written directly in the native language
+// of the store. We can use special features at the cost of portability.
+// 2. Instead of annotating the domain class we should be using @Query annotation at the query method
+// because it should be cleaner :)
+// So you'd better use @Query.
+// http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query
+// See: de.spring.persistence.example.repository.AdRepository
+//@NamedNativeQueries(
+// {
+// @NamedNativeQuery(
+// name="Ad.findByIdNativeQuery",
+// query="SELECT * FROM ad WHERE ad.id = :id",
+// resultClass=Ad.class)
+// }
+//)
+public class Ad implements Serializable {
+
+ @Id
+ @GeneratedValue(strategy=GenerationType.IDENTITY)
+ @Column(name="id", updatable=false, nullable=false)
+ private Long id;
+
+ @OneToMany(mappedBy="ad", fetch=FetchType.LAZY, targetEntity=AdDescription.class)
+ private Set<AdDescription> adDescriptions;
+
+ @Max(60)
+ @Column(name="company_id")
+ private Long companyId;
+
+ @Max(40)
+ @Column(name="company_categ_id")
+ private Long companyCategId;
+
+ @Size(min=2, max=255)
+ @Column(name="ad_mobile_image")
+ private String adMobileImage;
+
+ @NotNull
+ @Convert(converter=OffsetDateTimeAttributeConverter.class)
+ @Column(name="created_at", nullable=false)
+ @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ssZ")
+ private OffsetDateTime createdAt;
+
+ @NotNull
+ @Convert(converter=OffsetDateTimeAttributeConverter.class)
+ @Column(name="updated_at", nullable = false)
+ @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ssZ")
+ private OffsetDateTime updatedAt;
+
+ // It will be used by JPA when filling the property fields with data coming from data base.
+ protected Ad() {
+
+ }
+
+ // It will be used by my code (for example by Unit Tests)
+ public Ad(Long id, Long companyId, Long companyCategId, String adMobileImage, OffsetDateTime createdAt,
+ OffsetDateTime updatedAt) {
+ this.id = id;
+ this.companyCategId = companyCategId;
+ this.adMobileImage = adMobileImage;
+ this.createdAt = createdAt;
+ this.updatedAt = updatedAt;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public Long getCompanyId() {
+ return companyId;
+ }
+
+ public Long getCompanyCategId() {
+ return companyCategId;
+ }
+
+ public String getAdMobileImage() {
+ return adMobileImage;
+ }
+
+ public OffsetDateTime getCreatedAt() {
+ return createdAt;
+ }
+
+ public OffsetDateTime getUpdatedAt() {
+ return updatedAt;
+ }
+}
--- /dev/null
+package de.spring.example.persistence.domain;
+
+import java.io.Serializable;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.ManyToOne;
+import javax.persistence.Table;
+import javax.validation.constraints.Max;
+import javax.validation.constraints.NotNull;
+import javax.validation.constraints.Size;
+
+import de.spring.example.persistence.domain.Ad;
+
+@Entity
+@Table(name="ad_description", schema="mybatis_example")
+public class AdDescription implements Serializable {
+
+ @Id
+ @GeneratedValue(strategy=GenerationType.IDENTITY)
+ @Column(name="id", updatable=false, nullable=false)
+ private Long id;
+
+ @NotNull
+ @ManyToOne(optional=false)
+ @JoinColumn(name="ad_id", referencedColumnName="id")
+ private Ad ad;
+
+ @NotNull
+ @Max(60)
+ @Column(name="language_id")
+ private Long languageId;
+
+ @NotNull
+ @Size(min=2, max=255)
+ @Column(name="ad_name")
+ private String adName;
+
+ @NotNull
+ @Size(min=2, max=255)
+ @Column(name="ad_description")
+ private String adDescription;
+
+ @NotNull
+ @Size(min=2, max=500)
+ @Column(name="ad_mobile_text")
+ private String adMobileText;
+
+ @NotNull
+ @Size(min=2, max=3000)
+ @Column(name="ad_link")
+ private String adLink;
+
+ // It will be used by JPA when filling the property fields with data coming from data base.
+ protected AdDescription() {
+
+ }
+
+ // It will be used by my code (for example by Unit Tests)
+ public AdDescription(Long id, Ad ad, Long languageId, String adName, String adDescription,
+ String adMobileText, String adLink) {
+ this.id = id;
+ this.languageId = languageId;
+ this.ad = ad;
+ this.adName = adName;
+ this.adDescription = adDescription;
+ this.adMobileText = adMobileText;
+ this.adLink = adLink;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public Ad getAd() {
+ return ad;
+ }
+
+ public Long getLanguageId() {
+ return languageId;
+ }
+
+ public String getAdName() {
+ return adName;
+ }
+
+ public String getAdDescription() {
+ return adDescription;
+ }
+
+ public String getAdMobileText() {
+ return adMobileText;
+ }
+
+ public String getAdLink() {
+ return adLink;
+ }
+}
--- /dev/null
+package de.spring.example.persistence.repository;
+
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.data.repository.PagingAndSortingRepository;
+
+import de.spring.example.persistence.domain.Ad;
+import de.spring.example.persistence.domain.AdDescription;
+
+public interface AdDescriptionRepository extends PagingAndSortingRepository<AdDescription, Long> {
+
+ // Custom Query method
+ Page<AdDescription> findByAd(Ad ad, Pageable pageable);
+}
--- /dev/null
+package de.spring.example.persistence.repository;
+
+import org.springframework.data.jpa.repository.Query;
+import org.springframework.data.repository.PagingAndSortingRepository;
+import org.springframework.data.repository.query.Param;
+
+import de.spring.example.persistence.domain.Ad;
+
+public interface AdRepository extends PagingAndSortingRepository<Ad, Long> {
+
+ // Named Native Query (using the native language of the store) It is not portable.
+ // See de.spring.persistence.example.domain.Ad
+ @Query(value="SELECT * FROM ad WHERE ad.id = :id", nativeQuery=true)
+ Ad findByIdNativeQuery(@Param("id") Long id);
+
+ // Named Query (using JPL) It is portable.
+ // See de.spring.persistence.example.domain.Ad
+ @Query("select a from Ad a where a.id = :id")
+ Ad findByIdQuery(@Param("id") Long id);
+}
--- /dev/null
+package de.spring.example.rest.controllers;
+
+import javax.inject.Inject;
+
+import org.resthub.web.controller.RepositoryBasedRestController;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import de.spring.example.persistence.domain.Ad;
+import de.spring.example.persistence.repository.AdRepository;
+
+@RestController
+@RequestMapping("/ads/")
+public class AdController extends RepositoryBasedRestController<Ad, Long, AdRepository> {
+
+ @Override
+ @Inject
+ public void setRepository(AdRepository repository) {
+ this.repository = repository;
+ }
+
+ // I do not have to do anything here because all I need is implemented by RepositoryBasedRestController :)
+}
+++ /dev/null
-package de.spring.persistence.converters;
-
-import java.sql.Timestamp;
-import java.time.LocalDateTime;
-import java.time.OffsetDateTime;
-import java.time.ZoneId;
-import java.time.ZonedDateTime;
-
-import javax.persistence.AttributeConverter;
-import javax.persistence.Converter;
-
-@Converter(autoApply = true)
-public class OffsetDateTimeAttributeConverter implements AttributeConverter<OffsetDateTime, Timestamp> {
-
- @Override
- public Timestamp convertToDatabaseColumn(OffsetDateTime offsetDateTime) {
- Timestamp timestamp = null;
-
- if (offsetDateTime != null) {
- timestamp = Timestamp.valueOf(offsetDateTime.toLocalDateTime());
- }
-
- return timestamp;
- }
-
- @Override
- public OffsetDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
- OffsetDateTime offsetDateTime = null;
-
- if (sqlTimestamp != null) {
- final LocalDateTime localDateTime = sqlTimestamp.toLocalDateTime();
- final ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.systemDefault());
- offsetDateTime = zonedDateTime.toOffsetDateTime();
- }
-
- return offsetDateTime;
- }
-
-}
+++ /dev/null
-package de.spring.persistence.example.domain;
-
-import java.io.Serializable;
-import java.time.LocalDateTime;
-import java.time.OffsetDateTime;
-import java.util.Set;
-
-import javax.persistence.Column;
-import javax.persistence.Convert;
-import javax.persistence.Entity;
-import javax.persistence.FetchType;
-import javax.persistence.GeneratedValue;
-import javax.persistence.GenerationType;
-import javax.persistence.Id;
-//import javax.persistence.NamedNativeQueries;
-//import javax.persistence.NamedNativeQuery;
-//import javax.persistence.NamedQueries;
-//import javax.persistence.NamedQuery;
-import javax.persistence.OneToMany;
-import javax.persistence.Table;
-import javax.validation.constraints.Max;
-import javax.validation.constraints.NotNull;
-import javax.validation.constraints.Size;
-
-import com.fasterxml.jackson.annotation.JsonFormat;
-
-import de.spring.persistence.converters.OffsetDateTimeAttributeConverter;
-
-@Entity
-@Table(name="ad", schema="mybatis_example")
-// 1. Named query is JPL. It is portable.
-// 2. Instead of annotating the domain class we should be using @Query annotation at the query method
-// because it should be cleaner :)
-// So you'd better use @Query.
-//http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query
-//See: de.spring.persistence.example.repository.AdRepository
-//@NamedQueries(
-// {
-// @NamedQuery(
-// name="Ad.findByIdQuery",
-// query="select a from Ad a where a.id = :id)
-// }
-//
-//)
-// 1. Native query IS NOT JPL. It is not portable and it is written directly in the native language
-// of the store. We can use special features at the cost of portability.
-// 2. Instead of annotating the domain class we should be using @Query annotation at the query method
-// because it should be cleaner :)
-// So you'd better use @Query.
-// http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query
-// See: de.spring.persistence.example.repository.AdRepository
-//@NamedNativeQueries(
-// {
-// @NamedNativeQuery(
-// name="Ad.findByIdNativeQuery",
-// query="SELECT * FROM ad WHERE ad.id = :id",
-// resultClass=Ad.class)
-// }
-//)
-public class Ad implements Serializable {
-
- @Id
- @GeneratedValue(strategy=GenerationType.IDENTITY)
- @Column(name="id", updatable=false, nullable=false)
- private Long id;
-
- @OneToMany(mappedBy="ad", fetch=FetchType.LAZY, targetEntity=AdDescription.class)
- private Set<AdDescription> adDescriptions;
-
- @Max(60)
- @Column(name="company_id")
- private Long companyId;
-
- @Max(40)
- @Column(name="company_categ_id")
- private Long companyCategId;
-
- @Size(min=2, max=255)
- @Column(name="ad_mobile_image")
- private String adMobileImage;
-
- @NotNull
- @Convert(converter=OffsetDateTimeAttributeConverter.class)
- @Column(name="created_at", nullable=false)
- @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ssZ")
- private OffsetDateTime createdAt;
-
- @NotNull
- @Convert(converter=OffsetDateTimeAttributeConverter.class)
- @Column(name="updated_at", nullable = false)
- @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ssZ")
- private OffsetDateTime updatedAt;
-
- // It will be used by JPA when filling the property fields with data coming from data base.
- protected Ad() {
-
- }
-
- // It will be used by my code (for example by Unit Tests)
- public Ad(Long id, Long companyId, Long companyCategId, String adMobileImage, OffsetDateTime createdAt,
- OffsetDateTime updatedAt) {
- this.id = id;
- this.companyCategId = companyCategId;
- this.adMobileImage = adMobileImage;
- this.createdAt = createdAt;
- this.updatedAt = updatedAt;
- }
-
- public Long getId() {
- return id;
- }
-
- public Long getCompanyId() {
- return companyId;
- }
-
- public Long getCompanyCategId() {
- return companyCategId;
- }
-
- public String getAdMobileImage() {
- return adMobileImage;
- }
-
- public OffsetDateTime getCreatedAt() {
- return createdAt;
- }
-
- public OffsetDateTime getUpdatedAt() {
- return updatedAt;
- }
-}
+++ /dev/null
-package de.spring.persistence.example.domain;
-
-import java.io.Serializable;
-
-import javax.persistence.Column;
-import javax.persistence.Entity;
-import javax.persistence.GeneratedValue;
-import javax.persistence.GenerationType;
-import javax.persistence.Id;
-import javax.persistence.JoinColumn;
-import javax.persistence.ManyToOne;
-import javax.persistence.Table;
-import javax.validation.constraints.Max;
-import javax.validation.constraints.NotNull;
-import javax.validation.constraints.Size;
-
-@Entity
-@Table(name="ad_description", schema="mybatis_example")
-public class AdDescription implements Serializable {
-
- @Id
- @GeneratedValue(strategy=GenerationType.IDENTITY)
- @Column(name="id", updatable=false, nullable=false)
- private Long id;
-
- @NotNull
- @ManyToOne(optional=false)
- @JoinColumn(name="ad_id", referencedColumnName="id")
- private Ad ad;
-
- @NotNull
- @Max(60)
- @Column(name="language_id")
- private Long languageId;
-
- @NotNull
- @Size(min=2, max=255)
- @Column(name="ad_name")
- private String adName;
-
- @NotNull
- @Size(min=2, max=255)
- @Column(name="ad_description")
- private String adDescription;
-
- @NotNull
- @Size(min=2, max=500)
- @Column(name="ad_mobile_text")
- private String adMobileText;
-
- @NotNull
- @Size(min=2, max=3000)
- @Column(name="ad_link")
- private String adLink;
-
- // It will be used by JPA when filling the property fields with data coming from data base.
- protected AdDescription() {
-
- }
-
- // It will be used by my code (for example by Unit Tests)
- public AdDescription(Long id, Ad ad, Long languageId, String adName, String adDescription,
- String adMobileText, String adLink) {
- this.id = id;
- this.languageId = languageId;
- this.ad = ad;
- this.adName = adName;
- this.adDescription = adDescription;
- this.adMobileText = adMobileText;
- this.adLink = adLink;
- }
-
- public Long getId() {
- return id;
- }
-
- public Ad getAd() {
- return ad;
- }
-
- public Long getLanguageId() {
- return languageId;
- }
-
- public String getAdName() {
- return adName;
- }
-
- public String getAdDescription() {
- return adDescription;
- }
-
- public String getAdMobileText() {
- return adMobileText;
- }
-
- public String getAdLink() {
- return adLink;
- }
-}
+++ /dev/null
-package de.spring.persistence.example.repository;
-
-import org.springframework.data.domain.Page;
-import org.springframework.data.domain.Pageable;
-import org.springframework.data.repository.PagingAndSortingRepository;
-
-import de.spring.persistence.example.domain.Ad;
-import de.spring.persistence.example.domain.AdDescription;
-
-public interface AdDescriptionRepository extends PagingAndSortingRepository<AdDescription, Long> {
-
- // Custom Query method
- Page<AdDescription> findByAd(Ad ad, Pageable pageable);
-}
+++ /dev/null
-package de.spring.persistence.example.repository;
-
-import org.springframework.data.jpa.repository.Query;
-import org.springframework.data.repository.PagingAndSortingRepository;
-import org.springframework.data.repository.query.Param;
-
-import de.spring.persistence.example.domain.Ad;
-
-public interface AdRepository extends PagingAndSortingRepository<Ad, Long> {
-
- // Named Native Query (using the native language of the store) It is not portable.
- // See de.spring.persistence.example.domain.Ad
- @Query(value="SELECT * FROM ad WHERE ad.id = :id", nativeQuery=true)
- Ad findByIdNativeQuery(@Param("id") Long id);
-
- // Named Query (using JPL) It is portable.
- // See de.spring.persistence.example.domain.Ad
- @Query("select a from Ad a where a.id = :id")
- Ad findByIdQuery(@Param("id") Long id);
-}
+++ /dev/null
-package de.spring.rest.controllers;
-
-import javax.inject.Inject;
-
-import org.resthub.web.controller.RepositoryBasedRestController;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-import de.spring.persistence.example.domain.Ad;
-import de.spring.persistence.example.repository.AdRepository;
-
-@RestController
-@RequestMapping("/ads/")
-public class AdController extends RepositoryBasedRestController<Ad, Long, AdRepository> {
-
- @Override
- @Inject
- public void setRepository(AdRepository repository) {
- this.repository = repository;
- }
-
- // I do not have to do anything here because all I need is implemented by RepositoryBasedRestController :)
-}
\r
<context:annotation-config />\r
\r
- <context:component-scan base-package="de.spring.persistence, org.resthub" />\r
+ <context:component-scan base-package="de.spring.example.persistence, org.resthub" />\r
\r
<context:property-placeholder location="classpath:jpa.properties" />\r
\r
<property name="showSql" value="${jpa.show_sql}" />\r
</bean>\r
</property>\r
- <property name="packagesToScan" value="de.spring.persistence.**.domain" />\r
+ <property name="packagesToScan" value="de.spring.example.persistence.**.domain" />\r
</bean>\r
\r
<jpa:repositories entity-manager-factory-ref="entityManagerFactory"\r
- base-package="de.spring.persistence.**.repository"\r
+ base-package="de.spring.example.persistence.**.repository"\r
transaction-manager-ref="transactionManager" />\r
\r
</beans>\r
<context:annotation-config />
- <context:component-scan base-package="de.spring.rest"/>
+ <context:component-scan base-package="de.spring.example.rest"/>
<!--
Required beans for generating XML responses from Java objects using JAXB annotations