简体   繁体   中英

JPA executes the query but data not inserted

I am using play 2.3 and tying to persist an object the console shows the query but data is not inserted in db.

@Entity
@Table(name = "confrence_group")
public class ConfrenceGroup {
    @Id
    @Column(name = "id", length = 50, unique = true, nullable = false, insertable = false, updatable = false)
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Constraints.Required
    @Column(columnDefinition = "varchar(25) default ''")
    private String name;

    @Column(columnDefinition = "int default 0")
    private int active = 1;

    @JsonIgnore
    @ManyToOne
    private User user;

    @Formats.DateTime(pattern = "yyyy-MM-dd HH:mm:yy")
    @Column(columnDefinition = "TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP")
    private Date createdTime;

    @Column(unique = true)
    private String uid = (new UniqueToken()).nextSessionId();
}

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

    <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <non-jta-data-source>DefaultDS</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="javax.persistence.sql-load-script-source"
                      value="META-INF/sql/data.sql"/>
        </properties>

    </persistence-unit>

</persistence>

to persist I am doing

JPA.em().persist(confGroupObjectReference)

From Logging I can see

insert into confrence_group (active, createdTime, name, uid, user_id) values (1, '[SQL NULL of type 93]', 'akkk', '1474449665379l2urhrti496h0hoplm9cs55srj', 1)

Tried changing it to native query

String sql = "insert into confrence_group ( active , name , uid , user_id ) values (?, ?, ?, ?)";

   try {
        JPA.em().createNativeQuery(sql)
                .setParameter(1, 1)
                .setParameter(2, confo.getName())
                .setParameter(3, confo.getUid())
                .setParameter(4, user_id)
                .executeUpdate();
    } catch (Exception e) {
        e.printStackTrace();
    }

Logs

 insert into confrence_group (`active`, `name`, `uid`, `user_id`) values (1, 'akkk', '1474443561160ojedp25m5k01e5k1kc7jg39l6p', 1)

But for both cases data not inserted in table as I am checking through my database gui and there is no exception also. The code was working earlier but I dont know what caused it to behave like this

Can anybody help?

EDIT: tried creating new model but the problem is same.

看来您的ID生成是Mysql自动生成的,因此您需要将@GeneratedValue(strategy = GenerationType.AUTO)更改为@GeneratedValue(strategy=GenerationType.IDENTITY)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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