49

以下のように、2 つのドメイン モデルと 1 つの Spring REST コントローラーがあります。

@Entity
public class Customer{

@Id
private Long id;

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="COUNTRY_ID", nullable=false)
private Country country;

// other stuff with getters/setters

}

@Entity
public class Country{

@Id
@Column(name="COUNTRY_ID")
private Integer id;

// other stuff with getters/setters

}

スプリング REST コントローラー:

@Controller
@RequestMapping("/shop/services/customers")
public class CustomerRESTController {

   /**
    * Create new customer
    */
    @RequestMapping( method=RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    @ResponseBody
    public com.salesmanager.web.entity.customer.Customer createCustomer(@Valid @RequestBody   Customer customer, Model model, HttpServletRequest request, HttpServletResponse response) throws Exception {

        customerService.saveOrUpdate(customer);

        return customer;
    }

    // other stuff
}

以下のJSONを本体として、上記のRESTサービスを呼び出そうとしています:

{
    "firstname": "Tapas",
    "lastname": "Jena",
    "city": "Hyderabad",
    "country": "1"
}

国コード 1 は既に Country テーブルにあります。問題は、このサービスを呼び出しているときにエラーが発生することです:

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: com.test.model.Customer.country -> com.test.model.Country; nested exception is java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: com.test.model.Customer.country -> com.test.model.Country

どんな助けでも大歓迎です!

4

5 に答える 5

15

同様の問題がありました。2 つのエンティティ: DocumentStatusDocumentは、 Documentが持っていたStatusの履歴を表すStatusとの関係OneToManyを持っていました。

それで、Status内にDocument@NotNull @ManyToOneの参照がありました。

また、 Documentの実際のStatusを知る必要がありました。そのため、今度は Document 内に別の関係が必要でした。@OneToOne@NotNull

@NotNull問題は、両方が他方への参照を持っている場合、最初に両方のエンティティを永続化するにはどうすればよいかということでした。

@NotNull解決策は、参照から参照を削除することでしたactualStatus。このようにして、両方のエンティティを永続化することができました。

于 2014-09-11T19:46:20.227 に答える
-2

変更する必要があります:

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="COUNTRY_ID", nullable=false)
private Country country;

に :

@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name="COUNTRY_ID")
private Country country;

nullable設定を削除するだけです。

于 2018-04-20T10:20:27.443 に答える