database - Java JPA - Persisting @OneToMany entity -
hello everyone,
i'm having these days problem persisting object @onetomany. having problem i'm trying create both objects (one onetomany, other 1 manytoone), , want persist @onetomany one, , automatically have @manytoone persisted database also. (all tables set autoincrement, , collection initialized in constructor of komponenta.class, i've put parts connected problem). here's code:
public class komponenta implements serializable { @id private integer idkom; @onetomany(cascade = cascadetype.all, mappedby = "idkom") private collection<ima> imacollection = new arraylist<>(); } public class ima implements serializable { @id private integer idima; @joincolumn(name = "idkom", referencedcolumnname = "idkom") @manytoone private komponenta idkom; } // runnable part tools.em.gettransaction().begin(); komponenta k = new komponenta(); ima = new ima(); collection<ima> list = k.getimacollection(); list.add(i); k.setimacollection(list); i.setidkom(k); tools.em.persist(k); tools.em.gettransaction().commit();
i'm getting type of error console output:
internal exception: com.mysql.jdbc.exceptions.jdbc4.mysqlintegrityconstraintviolationexception: column 'idkom' cannot null error code: 1048
to use mysql auto_increment column, supposed use identity strategy:
@id @generatedvalue(strategy=generationtype.identity) private integer idkom;
which you'd when using auto mysql:
@id @generatedvalue(strategy=generationtype.auto) private integer idkom;
which equivalent to
@id @generatedvalue private integer idkom;
Comments
Post a Comment