- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在保存具有 ManyToOne 双向关系的实体时遇到问题,这里是:
警告:HHH000437:正在尝试保存一个或多个与未保存的 transient 实体具有不可空关联的实体。在保存这些依赖实体之前,必须在操作中保存未保存的 transient 实体。未保存的临时实体:([com.epam.apartmentsbooking.model.City#912])依赖实体:([[com.epam.apartmentsbooking.model.Apartment#640]])不可为 null 的关联:([com.epam.apartmentsbooking.model.Apartment.city])
org.hibernate.TransientPropertyValueException:非空属性引用 transient 值 - transient 实例必须在当前操作之前保存:com.epam.apartmentsbooking.model.Apartment.city -> com.epam.apartmentsbooking.model .城市
这看起来很常见,但我无法在数据库中保存引用现有城市的实体。
这是我的代码
Apartment.java:
package com.epam.apartmentsbooking.model;
import java.io.Serializable;
/**
* <p>
* Class describes the apartment entity
*/
public class Apartment implements Serializable {
private Long id;
private String name;
private String accommodationType;
private Byte numberOfGuests;
private Byte numberOfBedrooms;
private Byte numberOfBathrooms;
private Float pricePerDay;
private Float square;
private Short floor;
private String description;
private City city;
/**
* Default constructor of the class
* Should be used only when properties will be set later
*/
public Apartment() {
id = -1L;
name = "Default name";
accommodationType = null;
numberOfGuests = 0;
numberOfBedrooms = 0;
numberOfBathrooms = 0;
pricePerDay = 0F;
square = 0F;
floor = 0;
description = "Default description";
city = new City();
}
/**
* Counstructor that fully makes the object, should be used in most cases
*
* @param id ID of the apartment
* @param name Name of the apartment
* @param accommodationType Accommodation type (House, Apartment, Studio Apartment)
* @param numberOfGuests Maximum number of guests that host can place in the apartment
* @param numberOfBedrooms Number of bedrooms in the apartment
* @param numberOfBathrooms Number of bathrooms in the apartment
* @param pricePerDay Price per day for the apartment
* @param square Square of the apartment (in sq. m.)
* @param floor Floor on which apartment is located
* @param description Description of the apartment
* @param city City where apartment is located
*/
public Apartment(Long id, String name, String accommodationType, Byte numberOfGuests, //NOSONAR
Byte numberOfBedrooms, Byte numberOfBathrooms, Float pricePerDay, Float square, Short floor,
String description, City city) {
this.id = id;
this.name = name;
this.accommodationType = accommodationType;
this.numberOfGuests = numberOfGuests;
this.numberOfBedrooms = numberOfBedrooms;
this.numberOfBathrooms = numberOfBathrooms;
this.pricePerDay = pricePerDay;
this.square = square;
this.floor = floor;
this.description = description;
this.city = city;
}
/**
* @return the id of the apartment
*/
public Long getId() {
return id;
}
/**
* @param id id of the apartment to be set
*/
public void setId(Long id) {
this.id = id;
}
/**
* @return the name of the apartment
*/
public String getName() {
return name;
}
/**
* @param name the name to be set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Accommodation type of the apartment
*/
public String getAccommodationType() {
return accommodationType;
}
/**
* @param accomodationType accommodationType to be set
*/
public void setAccommodationType(String accomodationType) {
this.accommodationType = accomodationType;
}
/**
* @return the maximum number of guest that may be placed in the apartment
*/
public Byte getNumberOfGuests() {
return numberOfGuests;
}
/**
* @param numberOfGuests number of guests to be set
*/
public void setNumberOfGuests(Byte numberOfGuests) {
this.numberOfGuests = numberOfGuests;
}
/**
* @return the number of bedrooms
*/
public Byte getNumberOfBedrooms() {
return numberOfBedrooms;
}
/**
* @param numberOfBedrooms the number of bedrooms to be set
*/
public void setNumberOfBedrooms(Byte numberOfBedrooms) {
this.numberOfBedrooms = numberOfBedrooms;
}
/**
* @return the number of bathrooms
*/
public Byte getNumberOfBathrooms() {
return numberOfBathrooms;
}
/**
* @param numberOfBathrooms the number of bathrooms to be set
*/
public void setNumberOfBathrooms(Byte numberOfBathrooms) {
this.numberOfBathrooms = numberOfBathrooms;
}
/**
* @return the price of stay per day
*/
public Float getPricePerDay() {
return pricePerDay;
}
/**
* @param pricePerDay the price to be set
*/
public void setPricePerDay(Float pricePerDay) {
this.pricePerDay = pricePerDay;
}
/**
* @return the square of the apartment in square meters
*/
public Float getSquare() {
return square;
}
/**
* @param square square of the apartment to be set
*/
public void setSquare(Float square) {
this.square = square;
}
/**
* @return floor number of the apartment
*/
public Short getFloor() {
return floor;
}
/**
* @param floor number of floor to be set
*/
public void setFloor(Short floor) {
this.floor = floor;
}
/**
* @return the description of the apartment
*/
public String getDescription() {
return description;
}
/**
* @param description description to be set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return the city of the apartment
*/
public City getCity() {
return city;
}
/**
* @param city city to be set
*/
public void setCity(City city) {
this.city = city;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Apartment apartment = (Apartment) o;
if (!id.equals(apartment.id)) return false;
if (!name.equals(apartment.name)) return false;
if (accommodationType.equals(apartment.accommodationType)) return false;
if (!numberOfGuests.equals(apartment.numberOfGuests)) return false;
if (!numberOfBedrooms.equals(apartment.numberOfBedrooms)) return false;
if (!numberOfBathrooms.equals(apartment.numberOfBathrooms)) return false;
if (!pricePerDay.equals(apartment.pricePerDay)) return false;
if (!square.equals(apartment.square)) return false;
if (!floor.equals(apartment.floor)) return false;
if (!description.equals(apartment.description)) return false;
return city.equals(apartment.city);
}
@Override
public int hashCode() {
int result = id.hashCode();
result = 31 * result + name.hashCode();
result = 31 * result + accommodationType.hashCode();
result = 31 * result + numberOfGuests.hashCode();
result = 31 * result + numberOfBedrooms.hashCode();
result = 31 * result + numberOfBathrooms.hashCode();
result = 31 * result + pricePerDay.hashCode();
result = 31 * result + square.hashCode();
result = 31 * result + floor.hashCode();
result = 31 * result + description.hashCode();
result = 31 * result + city.hashCode();
return result;
}
@Override
public String toString() {
return "Apartment{" +
"id=" + id +
", name='" + name + '\'' +
", accomodationType=" + accommodationType +
", numberOfGuests=" + numberOfGuests +
", numberOfBedrooms=" + numberOfBedrooms +
", numberOfBathrooms=" + numberOfBathrooms +
", pricePerDay=" + pricePerDay +
", square=" + square +
", floor=" + floor +
", description='" + description + '\'' +
", city=" + city +
'}';
}
城市.java:
package com.epam.apartmentsbooking.model;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
/**
* <p>
* Class describes city entity
*/
public class City implements Serializable {
private Long id;
private String name;
private Country country;
private Set<Apartment> apartments = new HashSet<>();
/**
* Default constructor
* Preferable not to use
*/
public City() {
id = 912L;
name = "Minsk";
country = new Country();
}
/**
* Creates new city object
* Should be used in most cases
*
* @param id Id of the city
* @param name Name of the city
* @param country ISO Code of the country
*/
public City(Long id, String name, Country country) {
this.id = id;
this.name = name;
this.country = country;
}
/**
* @return the name of the city
*/
public String getName() {
return name;
}
/**
* @param name The name of the city to be set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Country object which the city is references on
*/
public Country getCountry() {
return country;
}
/**
* @param country Country object to set
*/
public void setCountry(Country country) {
this.country = country;
}
/**
* @return id of the city
*/
public Long getId() {
return id;
}
/**
* @param id id to be set
*/
public void setId(Long id) {
this.id = id;
}
public Set<Apartment> getApartments() {
return apartments;
}
public void setApartments(Set<Apartment> apartments) {
this.apartments = apartments;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
City city = (City) o;
if (id != null ? !id.equals(city.id) : city.id != null) return false;
if (name != null ? !name.equals(city.name) : city.name != null) return false;
if (country != null ? !country.equals(city.country) : city.country != null) return false;
return apartments != null ? apartments.equals(city.apartments) : city.apartments == null;
}
@Override
public int hashCode() {
int result = id != null ? id.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (country != null ? country.hashCode() : 0);
result = 31 * result + (apartments != null ? apartments.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "City{" +
"id=" + id +
", name='" + name + '\'' +
", country=" + country +
", apartments=" + apartments +
'}';
}
ApartmentDaoImpl.java:
package com.epam.apartmentsbooking.dao.impl.hibernate;
import com.epam.apartmentsbooking.dao.ApartmentDao;
import com.epam.apartmentsbooking.model.Apartment;
import com.epam.apartmentsbooking.model.City;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.Collection;
/**
*
* Apartment DAO Implementation
*
* @see com.epam.apartmentsbooking.dao.ApartmentDao
*/
@Repository("apartmentDaoHibernate")
@Transactional
public class ApartmentDaoImpl extends GenericDaoImpl<Apartment, Long> implements ApartmentDao {
@Autowired
@Qualifier("sessionFactory")
private SessionFactory sessionFactory;
@Override
public Long create(Apartment newInstance) {
return (Long) sessionFactory.getCurrentSession().save(newInstance);
}
@Override
public Apartment getByName(String name) {
Query query = sessionFactory.getCurrentSession().createQuery("from Apartment where name=:name");
query.setParameter("name", name);
return (Apartment) query.uniqueResult();
}
@Override
public Collection<Apartment> getAll() {
return sessionFactory.getCurrentSession().createCriteria(Apartment.class).list();
}
@Override
public Collection<Apartment> filter(String name, String accommodationType, Byte numberOfGuests,
Byte numberOfBedrooms, Byte numberOfBathrooms, Float pricePerDay,
Long cityId) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Apartment.class);
criteria.add(Restrictions.ilike("name", name + "%"));
criteria.add(Restrictions.ilike("accommodationType", accommodationType + "%"));
criteria.add(Restrictions.ge("numberOfGuests", numberOfGuests));
criteria.add(Restrictions.ge("numberOfBedrooms", numberOfBedrooms));
criteria.add(Restrictions.ge("numberOfBathrooms", numberOfBathrooms));
criteria.add(Restrictions.le("pricePerDay", pricePerDay));
criteria.add(Restrictions.eq("city.id", cityId));
return criteria.list();
}
测试用例
@Test
@ExpectedDatabase("/apartments-test-create.xml")
public void testCreate() {
Apartment newApartment = new Apartment(null, "Test Apartment#4", "STUDIO", (byte) 3, (byte) 3, (byte) 3,
400F, 78F, (short) 4, "EPAM Studio #2", new City());
apartmentDao.create(newApartment);
}
最佳答案
在您的 Apartment 类中,您必须告诉 hibernate 也可以将提交级联到“City”对象。您可以通过将 @Cascade 注释添加到 City 对象来执行此操作,如下所示:
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
private City city;
现在,当您保留公寓时,也会同时保留城市。
或者,首先保留您的“城市”对象,然后使用保留的城市对象构建公寓对象。
关于java - Hibernate ManyToOne 保存对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45486361/
什么是 hibernate 和n- hibernate ?我可以在 Visual Studio 2008 中使用它进行 C# Web 应用程序开发吗?请给我建议...我是 asp.net Web 应用
我有一个不系统地发生的异常(exception)。 我试图通过在每次迭代中刷新和清理 session 来解决此问题,但没有成功。 [quartzScheduler_Worker-7] ERROR jd
使用 Hibernate 在数据库中存储 IP 地址的最佳类型是什么? 我虽然是 Byte[] 或 String,但有没有更好的方法,或者你用什么? @Column(name = "range_fr
我正在尝试制定一个公式来选择用户个人资料的用户友好名称。它选择名字 + ' ' + 姓氏 如果其中至少有一个不为空且不为空(包含非空白字符),否则选择 短名称 (条件相同),最后,如果 短名称 为空或
在hibernate中,是否可以将鉴别器作为一个实体?例如,如果我将 Department 作为基类,将 AdminDepartment 和 ProcessingDepartment 作为子类。 De
我只想从表中获取一些列值。因此,我已经使用投影来实现这一目标。该代码有效,但我认为它无效。 我的问题是当我使用ProjectionsList并将标准条件列表设置为ArrayList时-Bulletin
你好: 我对 hibernate 缓存缓存的内容感到困惑。 从文档中,我知道 hibernate 中有缓存类型。 一级 :交易级别。 似乎要被 session 持久化的实体被缓存在这里。 二级缓存 :
我遇到了一个情况: save或update hibernate 的目标表中的某些数据 在目标表上有一个触发器,该触发器将在目标表的insert或update操作之前执行 由 hibernate 将此记
我有一个名为 Master_Info_tbl 的表。它是一个查询表: 这是该表的代码: @Entity @Table(name="MASTER_INFO_T") public class Code
我想知道如何在 Hibernate 查询语言中使用日期文字。我在我的 JPA 项目中做了如下操作(作为 Eclipselink 提供者)并且它工作正常。 SELECT m FROM Me m WHER
@Entity public class Troop { @OneToMany(mappedBy="troop") public Set getSoldiers() { ...
我正在尝试使用 hibernate 查询删除表 'user_role' 中的所有行。但每次我都会出错。有人可以帮我吗。 DaoImpl @Override public void deleteAll(
不是将数据库操作分散在四个 (osgi) 包中,而是在那里做略有不同的事情。我想创建一个负责所有持久性问题的(简单的)OSGi 包。我觉得这并不像听起来那么简单,因为“每个包都有独特的类加载器”。 因
这就是我使用生成器的方式: private Integer id; 我看到的行为是: 创建第一个对象 hibernate 分配 id = 1 删除该对象 关闭服务
对象级别的实体和值类型有什么区别。我知道实体将有一个 id 但值不会,但为什么我们需要不同的方法来映射实体与值类型? 这样做是为了让hibernate可以对值类型应用任何优化吗? 最佳答案 一个实体已
我正在使用 HibernateTemplate.findByCriteria 方法进行一些查询。现在我想在标准上创建一些 SQL 限制,比如 criteria.add(Restrictions.sql
所以我有以下代码: Query query = session.createQuery("from Weather"); List list = query.list();
如何使用Hibernate映射具有多个实体的 View ? 问候, 混沌 最佳答案 请参见Hibernate文档中第5.1.3节“类”,紧接在“Id”节之前: There is no differen
据我所知,Hibernate 有两种类型的实现 JPA的实现(2)(@Entity,@Table注解) 扩展到旧的(传统的) hibernate (没有 JPA),使用 HSQL 查询,没有注释 如果
我需要一个将条目存储为键值对的集合(因此我可以通过键查找值),但我需要一个允许多个值使用 hibernate 共享同一个键的集合 最佳答案 一个键具有多个值的映射称为多映射 - 在 Apache 公共
我是一名优秀的程序员,十分优秀!