Tag: jpa

hibernate:根据实体类自动创build/更新数据库表

我有以下实体类(在Groovy中): import javax.persistence.Entity import javax.persistence.Id import javax.persistence.GeneratedValue import javax.persistence.GenerationType @Entity public class ServerNode { @Id @GeneratedValue(strategy = GenerationType.AUTO) Long id String firstName String lastName } 和我的persistence.xml: <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> <persistence-unit name="NewPersistenceUnit"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/Icarus"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.password" value=""/> <property name="hibernate.archive.autodetection" value="class"/> <property name="hibernate.show_sql" value="true"/> <property […]

如何使用JPA和Hibernate在UTC时区存储date/时间和时间戳

如何configurationJPA / Hibernate作为UTC(GMT)时区在数据库中存储date/时间? 考虑这个带注释的JPA实体: public class Event { @Id public int id; @Temporal(TemporalType.TIMESTAMP) public java.util.Date date; } 如果date是2008年2月3日上午9:30太平洋标准时间(PST),那么我希望2008年2月3日下午5:30的UTC时间存储在数据库中。 同样,当从数据库中检索date时,我希望它解释为UTC。 所以在这种情况下,530pm是UTC530。 显示时,它将被格式化为太平洋标准时间上午9:30。

hibernateJPA序列(非Id)

是否有可能使用一个DB序列的某些列不是标识符/不是一个复合标识符的一部分 ? 我使用hibernate作为jpa提供程序,并且我有一个表有一些生成值的列(使用序列),尽pipe它们不是标识符的一部分。 我想要的是使用一个序列来创build一个实体的新值,其中序列的列不是主键的一部分: @Entity @Table(name = "MyTable") public class MyEntity { //… @Id //… etc public Long getId() { return id; } //note NO @Id here! but this doesn't work… @GeneratedValue(strategy = GenerationType.AUTO, generator = "myGen") @SequenceGenerator(name = "myGen", sequenceName = "MY_SEQUENCE") @Column(name = "SEQ_VAL", unique = false, nullable = false, insertable = true, […]

在弹簧引导jpahibernate时,连接到Db后大于4 <24

我有一个应用程序,使用spring-boot,jpa-hiberanate和mysql.I得到这个错误日志 Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 56,006,037 milliseconds ago. The last packet sent successfully to the server was 56,006,037 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured […]

将postgreSQL JSON列映射到Hibernate值types

我在postgreSQL数据库(9.2)有一个jsontypes的列表。 我很难将此列映射到JPA2实体字段types。 我试图使用string,但是当我保存实体时,我得到一个exception,它不能将字符变换为JSON。 处理JSON列时使用的正确的值types是什么? @Entity public class MyEntity { private String jsonPayload; // this maps to a json column public MyEntity() { } } 一个简单的解决方法是定义一个文本列。

JPA CascadeType.ALL不会删除孤儿

我在使用JPA删除孤立节点时遇到了以下映射问题 @OneToMany (cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "owner") private List<Bikes> bikes; 数据库中存在孤立angular色的问题。 我可以使用注释org.hibernate.annotations.Cascade Hibernate特定的标签,但显然我不想将我的解决scheme绑定到Hibernate实现。 编辑 :看来JPA 2.0将包括对此的支持。

使用JPA创build具有保留字名称的字段

@Column(name="open") 用hibernate的sqlserver方言。 [SchemaUpdate] Unsuccessful: create table auth_session (id numeric(19,0) identity not null, active tinyint null, creation_date datetime not null, last_modified datetime not null, maxidle int null, maxlive int null, open tinyint null, sessionid varchar(255) not null, user_id numeric(19,0) not null, primary key (id), unique (sessionid)) [SchemaUpdate] Incorrect syntax near the keyword 'open'. 我会希望hibernate在创build表时使用带引号的标识符。 任何想法如何处理这个…除了重命名字段?

JPA 2.0,Criteria API,子查询,expression式

我试图用子查询和一个INexpression式多次写查询语句。 但我从来没有成功。 我总是得到exception,“附近关键字”IN“的语法错误”,查询语句是这样构build的, SELECT t0.ID, t0.NAME FROM EMPLOYEE t0 WHERE IN (SELECT ? FROM PROJECT t2, EMPLOYEE t1 WHERE ((t2.NAME = ?) AND (t1.ID = t2.project))) “IN”输之前我知道这个词。 你有没有写过这样的查询? 任何build议?

ConcurrentModificationException和一个HashMap

我正在使用JPA持久对象。 主对象与另一个对象拥有一对多的关系。 另一个对象存储在一个HashMap中。 什么样的同步可以解决这个问题? 这似乎发生在完全随机的时间,是非常不可预知的。 这是我得到的例外: Exception in thread "pool-1-thread-1" java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry(Unknown Source) at java.util.HashMap$ValueIterator.next(Unknown Source) at org.hibernate.collection.AbstractPersistentCollection$IteratorProxy.next(AbstractPersistentCollection.java:555) at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296) at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242) at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219) at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169) at org.hibernate.engine.Cascade.cascade(Cascade.java:130)

使用额外列映射多对多关联表

我的数据库包含3个表:用户和服务实体具有多对多的关系,并与SERVICE_USER表连接如下: 用户 – SERVICE_USER – 服务 SERVICE_USER表包含额外的BLOCKED列。 执行这种映射的最佳方法是什么? 这些是我的实体类 @Entity @Table(name = "USERS") public class User implements java.io.Serializable { private String userid; private String email; @Id @Column(name = "USERID", unique = true, nullable = false,) public String getUserid() { return this.userid; } …. some get/set methods } @Entity @Table(name = "SERVICES") public class CmsService implements […]