当前位置: 首页 > 知识库问答 >
问题:

HiberNate ManyToOne保存对象

董建茗
2023-03-14

我在保存具有ManyToOne双向关系的实体时遇到问题,如下所示:

警告:HHH000437:试图保存一个或多个与未保存的瞬态实体具有不可为空的关联的实体。在保存这些从属实体之前,必须在操作中保存未保存的瞬态实体。未保存的临时实体:([com . epam . apartments booking . model . city # 912])依赖实体:([[com . epam . apartments booking . model . apartment # 640]])不可为空的关联:([com . epam . apartments booking . model . apartment . city])

有机纤维素。TransientPropertyValueException:非空属性引用了一个瞬态值-必须在当前操作之前保存瞬态实例:com.epam.apartmentbooking.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 +
            '}';
}

}

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 +
            '}';
}

}

公寓DaoImpl.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);
    }

共有1个答案

余弘新
2023-03-14

在Apartment类中,您必须告诉hibernate,也可以将提交级联到“City”对象。您可以通过将@Cascade注释添加到City对象来完成此操作,如下所示:

@Cascade({ org.hibernate.annotations.CascadeType.ALL })
private City city;

现在,当你坚持公寓,那也将坚持城市的同时。

或者,首先持久化您的“城市”对象,然后使用持久化的城市对象来构建公寓对象。

 类似资料:
  • 我正在学习SpringJPA,我从将对象映射到表开始。我对OneToOne和OneToMany的关系没有问题,但我不太明白为什么我不能预先列出一个与许多关系相关的对象。我有一本可以有很多作者的书,还有一本可以有很多书的作者。通过中间表book_author正确生成模式(我可以在MySql Workbench中看到),但我得到以下示例: Hibernate:在作者(名字、姓氏)中插入值(?,) Hi

  • 所以今天我正在为Minecraft服务器开发一个插件。为此,我创建了一个“Track”类,它表示一条赛车道。为了存储所有现有的轨道与他们的数据,我想保存整个轨道对象,这使一切对我更容易。总之,我在我的Track类中实现了Serializable,以便稍后使用ObjectOutputStream。当我开始保存轨迹时,Java创建了一个对象文件,但它也会给我一个警告/错误: 14:19:00][服务器

  • 当我读到cascade和inverse时,有人说它们都在做完全不同的事情。级联:在级联中,在一个操作(保存、更新和删除)完成后,它决定是否需要调用其他操作(保存、更新和删除)对其他相互有关系的实体。反向:这用于决定哪一方是管理关系的关系所有者(插入或更新外键列)。 假设我保存有一些孩子的父级。所以当我一方面用inverse="true"cascade="all"设置一对多时,我希望孩子是关系的所有

  • 问题内容: 我已经创建了一个JSON文件,并且需要能够通过电子邮件与其他协作者共享该文件。但是,尽管在R工作区中有许多处理JSON对象的主题,但实际上没有资源讨论如何实际将JSON对象导出到.JSON文件。 这是一个简单的例子: 最后一行,尝试读取JSON文件,导致错误:“ fromJSON(file =“ export.JSON”)中的错误:意外字符’R’” 显然,save()函数并不是可行的方