How to save a table without auto save other table when used @ManyToOne and @JoinColumn

51 views Asked by At

can you help me this problem please.

I created my own code like this in class Country.

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "region_id")
private Region region;

it is oke when I save a new Country. But, I let customer choose options about region by getting allRegion in database.

<div class="mb-3">
  <label for="listRegion" class="form-label">List region</label>
  <select class="form-control" th:field="${country.region.regionName}" id="listRegion">
    <option th:each="region:${regions}" th:value="${region.regionName}" th:text="${region.regionName}"></option>
  </select>
</div>

My problem is when I save new Country it auto add a new Region in my database. I just want to customer choose a list of Region and we will save it just on Country Here is diagram between Country and Region diagram between Country and Region This is my form to save Country

<form th:action="@{/countries/save}" th:object="${country}" method="post">
  <h3>Update country</h3>
  <div class="mb-3">
    <label for="countryid" class="form-label">Country Id</label>
    <input type="text" class="form-control" id="countryid" th:field="*{countryId}" aria-describedby="countryIdHelp">
    <div id="countryIdHelp" class="form-text">
    <span th:if="${#fields.hasErrors('countryId')}" th:errors="*{countryId}" class="text-danger"></span>
    </div>
  </div>
  <div class="mb-3">
    <label for="countryname" class="form-label">Country name</label>
    <input type="text" id="countryname" th:field="*{countryName}" class="form-control" aria-describedby="countryNameHelp">
    <div id="countryNameHelp" class="form-text">
      <span th:if="${#fields.hasErrors('countryName')}" th:errors="*{countryName}" class="text-danger"></span>
    </div>
  </div>
  <div class="mb-3">
    <label for="listRegion" class="form-label">List region</label>
    <select class="form-control" th:field="${country.region.regionName}" id="listRegion">
      <option th:each="region:${regions}" th:value="${region.regionName}" th:text="${region.regionName}"></option>
    </select>
  </div>
  <input type="submit" class="btn btn-primary">
</form>

How can I result this problem.

I tried to remove cascade but I got an error

  @ManyToOne
    @JoinColumn(name = "region_id")
    private Region region;`

There was an unexpected error (type=Internal Server Error, status=500). org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.vtlamdev.business.business.entity.Country.region -> com.vtlamdev.business.business.entity.Region org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : com.vtlamdev.business.business.entity.Country.region -> com.vtlamdev.business.business.entity.Region

1

There are 1 answers

0
Hassan Golshani On

First get region entity from db by Id and then save parent entity. something like this:

public Location save(LocationModel locationModel) {
    Region region = regionRepository.findById(locationModel.getRegionId());
    Location location = new Location();
    location.setTitle(locationModel.getTitle());
    //other location fields fill here
    location.setRegion(region);

    locationRepostiory.save(location);
    return location;
}