简体   繁体   English

休眠:分离的实体传递给持久化

[英]Hibernate: detached entity passed to persist

In my current spring project, when I try store in the database a record based on this entty: 在我当前的spring项目中,当我尝试将基于此实体的记录存储在数据库中时:

@Entity
@Table(name="produto")
public class Produto {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @Column(name="nome", nullable=false, unique=true)
    @Order(value=1)
    private String nome;

    @Column(name="preco", nullable=false)
    @Order(value=2)
    private Float preco;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name="categoria")
    @Cascade(value = { org.hibernate.annotations.CascadeType.ALL })
    @Order(value=3)
    private Categoria categoria;

    @Column(name="resumo", length=140)
    @Order(value=4)
    private String resumo;

    @Column(name="descricao", length=65535)
    @Order(value=5)
    private String descricao;
}

I am getting this error: http://www.klebermota.eti.br/wp-content/erro.html 我收到此错误: http : //www.klebermota.eti.br/wp-content/erro.html

The error is only displayed when I select an item in the select from the form below. 仅当我从下面的表单中选择一个项目时,才会显示该错误。 If I don't select an option, the record is stored without problem. 如果我不选择任何选项,则记录将毫无问题地存储。

<form id="command" class="form" action="/loja/Produto/cadastra" method="POST" enctype="multipart/form-data">
        <div class="row">
            <div class="col-md-3"></div>
            <div class="col-md-6"></div>
            <div class="col-md-3"></div>
        </div>
        <div class="row">
            <div class="col-md-3">
                <h3><label for="nome" class="label label-default">nome</label></h3>
            </div>
            <div class="col-md-6">
                <input id="nome" name="nome" class="form-control" type="text" value=""/>
            </div>
            <div class="col-md-3"></div>
        </div>
        <div class="row">
            <div class="col-md-3">
                <h3><label for="preco" class="label label-default">preco</label></h3>
            </div>
            <div class="col-md-6">
                <input id="preco" name="preco" pattern="[0-9]{3}.[0-9]{2}" class="form-control valida" type="text" value=""/>
            </div>
            <div class="col-md-3"></div>
        </div>
        <div class="row">
            <div class="col-md-3">
                <h3><label for="categoria" class="label label-default">categoria</label></h3>
            </div>
            <div class="col-md-3">

                <select id="categoria.id" name="categoria.id" class="form-control select categoria" data-nome="Categoria" data-lista="/loja/Produto/listagem3.json"></select>
            </div>
            <div class="col-md-3"></div>
        </div>
        <div class="row">
            <div class="col-md-3">
                <h3><label for="resumo" class="label label-default">resumo</label></h3>
            </div>
            <div class="col-md-6">
                <input id="resumo" name="resumo" class="form-control" type="text" value=""/>
            </div>
            <div class="col-md-3"></div>
        </div>
        <div class="row">
            <div class="col-md-3">
                <h3><label for="descricao" class="label label-default">descricao</label></h3>
            </div>
            <div class="col-md-6">
                <textarea id="descricao" name="descricao" class="form-control" rows="25" cols="50"></textarea>
            </div>
            <div class="col-md-3"></div>
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">&iacute;cone do produto (jpeg, 171x180)</div>
            <div class="panel-body">
                <input type="file" name="icone" class="form-control">
            </div>
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">imagem da capa (jpeg, 1280x250)</div>
            <div class="panel-body">
                <input type="file" name="file" class="form-control">
            </div>
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">capturas de tela (jpeg, 960x720)</div>
            <div class="panel-body">
                <p></p>
            </div>
        </div>
        <div class="row">
            <div class="col-md-3"> <button type="submit" class="btn btn-lg btn-primary">cadastrar</button> </div>
            <div class="col-md-3"></div>
        </div>
</form>

Anyone can tell me what's wrong here? 有人可以告诉我这是怎么回事吗? I am a bit stucked with this for a while. 我对此有点卡住了。

Can you try to set Categoria of your Produto by selecting from database by ID comes from client. 您可以通过按客户端提供的ID从数据库中进行选择来尝试设置Produto的类别吗? You are just inserting Produto but Categoria. 您只是插入Produto而不是Categoria。 So İn your situation Categoria is not persistable and should be. 因此,在您的情况下,类别不是持久的,应该持久。

You probably want to use merge instead of persist . 您可能想使用merge而不是persist persist can only insert a new record whereas merge will perform an insert or update. 持久只能插入一条新记录,而合并将执行插入或更新。 Your error occurs because, when they select an existing id, you actually want to do an update, not an insert. 发生您的错误是因为,当他们选择一个现有ID时,您实际上是要进行更新而不是插入。 Be sure to use the result returned from merge as the new value of your entity after you call it. 调用后,请确保将合并返回的结果用作实体的新值。

the working solution for this problem was this configuration for the class: 该问题的有效解决方案是该类的配置:

@Entity
@Table(name="produto")
public class Produto {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;

    @Column(name="nome", length=32, nullable=false, unique=true)
    @Order(value=1)
    private String nome;

    @Column(name="titulo", length=32, nullable=false)
    @Order(value=2)
    private String titulo;

    @Column(name="preco")
    @Order(value=3)
    private Float preco;

    @OneToOne
    @JoinColumn(name="categoria")
    @Order(value=4)
    private Categoria categoria;

    @Column(name="descricao", length=65535)
    @Order(value=5)
    private String descricao;

    @Column(name="destaque")
    @Order(value=6)
    private boolean destaque;
}

Now with this I can insert a Produto entity with one selected Categoria . 现在,我可以插入带有一个选定CategoriaProduto实体。 I am trying now solve the insertion without need to select a Categoria . 我现在尝试解决插入问题,而无需选择Categoria

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM