Drop back to Hibernate 5.0.9.Final
The upgrade to Hibernate 5.2.0.Final has provide to be too problematic to live with. It requires Java 8, is incompatible with a number of other projects in the Hibernate ecosystem, and it's unclear for how long it will be maintained. We'd previously used Hibernate 5.1.0.Final but its maintenance is also unclear with Hibernate 5.1.1.Final being more than 3 months overdue. This commit drops back to Hibernate 5.0.9.Final. This has a few advantages: - It's Java 7 compatible - It's had some time to mature and should be reasonably free of regressions for those moving from 4.3.x - It's used in both Wildfly and JBoss EAP so there's a fair chance that it will continue to be maintained. Closes gh-6198
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SampleHibernate52Application {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(SampleHibernate52Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.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;
|
||||
|
||||
@Entity
|
||||
public class City implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String name;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String state;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String country;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String map;
|
||||
|
||||
protected City() {
|
||||
}
|
||||
|
||||
public City(String name, String country) {
|
||||
super();
|
||||
this.name = name;
|
||||
this.country = country;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String getState() {
|
||||
return this.state;
|
||||
}
|
||||
|
||||
public String getCountry() {
|
||||
return this.country;
|
||||
}
|
||||
|
||||
public String getMap() {
|
||||
return this.map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getName() + "," + getState() + "," + getCountry();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
import org.hibernate.annotations.NaturalId;
|
||||
|
||||
@Entity
|
||||
public class Hotel implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne(optional = false)
|
||||
@NaturalId
|
||||
private City city;
|
||||
|
||||
@Column(nullable = false)
|
||||
@NaturalId
|
||||
private String name;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String address;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String zip;
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY, mappedBy = "hotel")
|
||||
private Set<Review> reviews;
|
||||
|
||||
protected Hotel() {
|
||||
}
|
||||
|
||||
public Hotel(City city, String name) {
|
||||
this.city = city;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public City getCity() {
|
||||
return this.city;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return this.address;
|
||||
}
|
||||
|
||||
public String getZip() {
|
||||
return this.zip;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.domain;
|
||||
|
||||
public interface HotelSummary {
|
||||
|
||||
City getCity();
|
||||
|
||||
String getName();
|
||||
|
||||
Double getAverageRating();
|
||||
|
||||
default Integer getAverageRatingRounded() {
|
||||
return getAverageRating() == null ? null : (int) Math.round(getAverageRating());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.domain;
|
||||
|
||||
public enum Rating {
|
||||
TERRIBLE, POOR, AVERAGE, GOOD, EXCELLENT,
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.domain;
|
||||
|
||||
public interface RatingCount {
|
||||
|
||||
Rating getRating();
|
||||
|
||||
long getCount();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Temporal;
|
||||
import javax.persistence.TemporalType;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@Entity
|
||||
public class Review implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@ManyToOne(optional = false)
|
||||
private Hotel hotel;
|
||||
|
||||
@Column(nullable = false, name = "idx")
|
||||
private int index;
|
||||
|
||||
@Column(nullable = false)
|
||||
@Enumerated(EnumType.ORDINAL)
|
||||
private Rating rating;
|
||||
|
||||
@Column(name = "CHECK_IN_DATE", nullable = false)
|
||||
@Temporal(TemporalType.DATE)
|
||||
private Date checkInDate;
|
||||
|
||||
@Column(name = "TRIP_TYPE", nullable = false)
|
||||
@Enumerated(EnumType.ORDINAL)
|
||||
private TripType tripType;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String title;
|
||||
|
||||
@Column(nullable = false, length = 5000)
|
||||
private String details;
|
||||
|
||||
protected Review() {
|
||||
}
|
||||
|
||||
public Review(Hotel hotel, int index, ReviewDetails details) {
|
||||
Assert.notNull(hotel, "Hotel must not be null");
|
||||
Assert.notNull(details, "Details must not be null");
|
||||
this.hotel = hotel;
|
||||
this.index = index;
|
||||
this.rating = details.getRating();
|
||||
this.checkInDate = details.getCheckInDate();
|
||||
this.tripType = details.getTripType();
|
||||
this.title = details.getTitle();
|
||||
this.details = details.getDetails();
|
||||
}
|
||||
|
||||
public Hotel getHotel() {
|
||||
return this.hotel;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return this.index;
|
||||
}
|
||||
|
||||
public Rating getRating() {
|
||||
return this.rating;
|
||||
}
|
||||
|
||||
public void setRating(Rating rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
public Date getCheckInDate() {
|
||||
return this.checkInDate;
|
||||
}
|
||||
|
||||
public void setCheckInDate(Date checkInDate) {
|
||||
this.checkInDate = checkInDate;
|
||||
}
|
||||
|
||||
public TripType getTripType() {
|
||||
return this.tripType;
|
||||
}
|
||||
|
||||
public void setTripType(TripType tripType) {
|
||||
this.tripType = tripType;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return this.title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDetails() {
|
||||
return this.details;
|
||||
}
|
||||
|
||||
public void setDetails(String details) {
|
||||
this.details = details;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
public class ReviewDetails implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Rating rating;
|
||||
|
||||
private Date checkInDate;
|
||||
|
||||
private TripType tripType;
|
||||
|
||||
private String title;
|
||||
|
||||
private String details;
|
||||
|
||||
public ReviewDetails() {
|
||||
}
|
||||
|
||||
public Rating getRating() {
|
||||
return this.rating;
|
||||
}
|
||||
|
||||
public void setRating(Rating rating) {
|
||||
this.rating = rating;
|
||||
}
|
||||
|
||||
public Date getCheckInDate() {
|
||||
return this.checkInDate;
|
||||
}
|
||||
|
||||
public void setCheckInDate(Date checkInDate) {
|
||||
this.checkInDate = checkInDate;
|
||||
}
|
||||
|
||||
public TripType getTripType() {
|
||||
return this.tripType;
|
||||
}
|
||||
|
||||
public void setTripType(TripType tripType) {
|
||||
this.tripType = tripType;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return this.title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getDetails() {
|
||||
return this.details;
|
||||
}
|
||||
|
||||
public void setDetails(String details) {
|
||||
this.details = details;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.domain;
|
||||
|
||||
public enum TripType {
|
||||
BUSINESS, COUPLES, FAMILY, FRIENDS, SOLO
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.service;
|
||||
|
||||
import sample.hibernate52.domain.City;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
interface CityRepository extends Repository<City, Long> {
|
||||
|
||||
Page<City> findAll(Pageable pageable);
|
||||
|
||||
Page<City> findByNameContainingAndCountryContainingAllIgnoringCase(String name,
|
||||
String country, Pageable pageable);
|
||||
|
||||
City findByNameAndCountryAllIgnoringCase(String name, String country);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.service;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
public class CitySearchCriteria implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String name;
|
||||
|
||||
public CitySearchCriteria() {
|
||||
}
|
||||
|
||||
public CitySearchCriteria(String name) {
|
||||
Assert.notNull(name, "Name must not be null");
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.service;
|
||||
|
||||
import sample.hibernate52.domain.City;
|
||||
import sample.hibernate52.domain.HotelSummary;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
public interface CityService {
|
||||
|
||||
Page<City> findCities(CitySearchCriteria criteria, Pageable pageable);
|
||||
|
||||
City getCity(String name, String country);
|
||||
|
||||
Page<HotelSummary> getHotels(City city, Pageable pageable);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.service;
|
||||
|
||||
import sample.hibernate52.domain.City;
|
||||
import sample.hibernate52.domain.HotelSummary;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@Component("cityService")
|
||||
@Transactional
|
||||
class CityServiceImpl implements CityService {
|
||||
|
||||
private final CityRepository cityRepository;
|
||||
|
||||
private final HotelRepository hotelRepository;
|
||||
|
||||
public CityServiceImpl(CityRepository cityRepository,
|
||||
HotelRepository hotelRepository) {
|
||||
this.cityRepository = cityRepository;
|
||||
this.hotelRepository = hotelRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<City> findCities(CitySearchCriteria criteria, Pageable pageable) {
|
||||
|
||||
Assert.notNull(criteria, "Criteria must not be null");
|
||||
String name = criteria.getName();
|
||||
|
||||
if (!StringUtils.hasLength(name)) {
|
||||
return this.cityRepository.findAll(null);
|
||||
}
|
||||
|
||||
String country = "";
|
||||
int splitPos = name.lastIndexOf(",");
|
||||
|
||||
if (splitPos >= 0) {
|
||||
country = name.substring(splitPos + 1);
|
||||
name = name.substring(0, splitPos);
|
||||
}
|
||||
|
||||
return this.cityRepository
|
||||
.findByNameContainingAndCountryContainingAllIgnoringCase(name.trim(),
|
||||
country.trim(), pageable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public City getCity(String name, String country) {
|
||||
Assert.notNull(name, "Name must not be null");
|
||||
Assert.notNull(country, "Country must not be null");
|
||||
return this.cityRepository.findByNameAndCountryAllIgnoringCase(name, country);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<HotelSummary> getHotels(City city, Pageable pageable) {
|
||||
Assert.notNull(city, "City must not be null");
|
||||
return this.hotelRepository.findByCity(city, pageable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import sample.hibernate52.domain.City;
|
||||
import sample.hibernate52.domain.Hotel;
|
||||
import sample.hibernate52.domain.HotelSummary;
|
||||
import sample.hibernate52.domain.RatingCount;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
interface HotelRepository extends Repository<Hotel, Long> {
|
||||
|
||||
Hotel findByCityAndName(City city, String name);
|
||||
|
||||
@Query("select h.city as city, h.name as name, avg(r.rating) as averageRating "
|
||||
+ "from Hotel h left outer join h.reviews r where h.city = ?1 group by h")
|
||||
Page<HotelSummary> findByCity(City city, Pageable pageable);
|
||||
|
||||
@Query("select r.rating as rating, count(r) as count "
|
||||
+ "from Review r where r.hotel = ?1 group by r.rating order by r.rating DESC")
|
||||
List<RatingCount> findRatingCounts(Hotel hotel);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.service;
|
||||
|
||||
import sample.hibernate52.domain.City;
|
||||
import sample.hibernate52.domain.Hotel;
|
||||
import sample.hibernate52.domain.Review;
|
||||
import sample.hibernate52.domain.ReviewDetails;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
|
||||
public interface HotelService {
|
||||
|
||||
Hotel getHotel(City city, String name);
|
||||
|
||||
Page<Review> getReviews(Hotel hotel, Pageable pageable);
|
||||
|
||||
Review getReview(Hotel hotel, int index);
|
||||
|
||||
Review addReview(Hotel hotel, ReviewDetails details);
|
||||
|
||||
ReviewsSummary getReviewSummary(Hotel hotel);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.service;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import sample.hibernate52.domain.City;
|
||||
import sample.hibernate52.domain.Hotel;
|
||||
import sample.hibernate52.domain.Rating;
|
||||
import sample.hibernate52.domain.RatingCount;
|
||||
import sample.hibernate52.domain.Review;
|
||||
import sample.hibernate52.domain.ReviewDetails;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@Component("hotelService")
|
||||
@Transactional
|
||||
class HotelServiceImpl implements HotelService {
|
||||
|
||||
private final HotelRepository hotelRepository;
|
||||
|
||||
private final ReviewRepository reviewRepository;
|
||||
|
||||
public HotelServiceImpl(HotelRepository hotelRepository,
|
||||
ReviewRepository reviewRepository) {
|
||||
this.hotelRepository = hotelRepository;
|
||||
this.reviewRepository = reviewRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Hotel getHotel(City city, String name) {
|
||||
Assert.notNull(city, "City must not be null");
|
||||
Assert.hasLength(name, "Name must not be empty");
|
||||
return this.hotelRepository.findByCityAndName(city, name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<Review> getReviews(Hotel hotel, Pageable pageable) {
|
||||
Assert.notNull(hotel, "Hotel must not be null");
|
||||
return this.reviewRepository.findByHotel(hotel, pageable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Review getReview(Hotel hotel, int reviewNumber) {
|
||||
Assert.notNull(hotel, "Hotel must not be null");
|
||||
return this.reviewRepository.findByHotelAndIndex(hotel, reviewNumber);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Review addReview(Hotel hotel, ReviewDetails details) {
|
||||
Review review = new Review(hotel, 1, details);
|
||||
return this.reviewRepository.save(review);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReviewsSummary getReviewSummary(Hotel hotel) {
|
||||
List<RatingCount> ratingCounts = this.hotelRepository.findRatingCounts(hotel);
|
||||
return new ReviewsSummaryImpl(ratingCounts);
|
||||
}
|
||||
|
||||
private static class ReviewsSummaryImpl implements ReviewsSummary {
|
||||
|
||||
private final Map<Rating, Long> ratingCount;
|
||||
|
||||
public ReviewsSummaryImpl(List<RatingCount> ratingCounts) {
|
||||
this.ratingCount = new HashMap<Rating, Long>();
|
||||
for (RatingCount ratingCount : ratingCounts) {
|
||||
this.ratingCount.put(ratingCount.getRating(), ratingCount.getCount());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getNumberOfReviewsWithRating(Rating rating) {
|
||||
Long count = this.ratingCount.get(rating);
|
||||
return count == null ? 0 : count;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.service;
|
||||
|
||||
import sample.hibernate52.domain.Hotel;
|
||||
import sample.hibernate52.domain.Review;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
interface ReviewRepository extends Repository<Review, Long> {
|
||||
|
||||
Page<Review> findByHotel(Hotel hotel, Pageable pageable);
|
||||
|
||||
Review findByHotelAndIndex(Hotel hotel, int index);
|
||||
|
||||
Review save(Review review);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.service;
|
||||
|
||||
import sample.hibernate52.domain.Rating;
|
||||
|
||||
public interface ReviewsSummary {
|
||||
|
||||
long getNumberOfReviewsWithRating(Rating rating);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package sample.hibernate52.web;
|
||||
|
||||
import sample.hibernate52.service.CityService;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
public class SampleController {
|
||||
|
||||
@Autowired
|
||||
private CityService cityService;
|
||||
|
||||
@GetMapping("/")
|
||||
@ResponseBody
|
||||
@Transactional(readOnly = true)
|
||||
public String helloWorld() {
|
||||
return this.cityService.getCity("Bath", "UK").getName();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user