小编Roo*_*oky的帖子

休眠:创建索引

我想在我的数据库中创建几个索引.不幸的是,我们必须将持久性提供程序从EclipseLink更改为Hibernate,但是使用javax.persistence.Index的解决方案也不能解决Hibernate的问题.

这就是这个类的样子:

@Entity
@Table(name = "my_shop")
public class Shop extends BaseEntity {

    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable = false)
    private Calendar lastUpdate;
}
Run Code Online (Sandbox Code Playgroud)

这应该是javax.persistence的解决方案.*:

import javax.persistence.Index;
import javax.persistence.Table;

@Table(name = "my_shop",
        indexes = @Index(columnList = "lastupdate")
)
Run Code Online (Sandbox Code Playgroud)

不推荐使用Hibernate注释,因此必须有理由不使用这些注释:

import org.hibernate.annotations.Index; // deprecated
import org.hibernate.annotations.Table;

@Table(...,
        indexes = @Index(columnNames = "lastupdate")
)
Run Code Online (Sandbox Code Playgroud)

我使用Glassfish 3.1.2.2,PostgreSQL 9.1,JPA 2.1和hibernate-core 4.3.4.Final.如果我查看数据库,则不会通过psql"\ d +"在特定字段上创建索引.

这就是我的persistence.xml的样子:

...
    <property name="hibernate.hbm2ddl.auto" value="create"/>
    <property name="dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
...
Run Code Online (Sandbox Code Playgroud)

只有EclipseLink可以轻松处理:

import org.eclipse.persistence.annotations.Index;

@Entity
@Table(name = "my_shop")
public class Shop extends BaseEntity {

    @Index
    @Temporal(TemporalType.TIMESTAMP)
    @Column(nullable …
Run Code Online (Sandbox Code Playgroud)

java postgresql hibernate jpa

17
推荐指数
4
解决办法
6万
查看次数

杰克逊:@JsonIdentityInfo对象而不是id

有没有办法用@JsonIdentityInfo影响序列化过程,以便它插入整个对象而不是引用id?

@Entity
@JsonIdentityInfo(
        generator = ObjectIdGenerators.IntSequenceGenerator.class,
        property = "linkLabel")
public class LinkLabel implements Serializable {
   //...
}
Run Code Online (Sandbox Code Playgroud)

因此,杰克逊应该包含整个对象,而不是引用id为1的"otherObj".

{
    "objects": [{
            "id": 1,
            "otherObj": [{
                    "id": 1,
                    ...
                }, {
                    "id": 3,
                    ...
                }]
        },
            "id": 2,
            "otherObj": [1] <-- referencing otherObj with id 1
    ]
}
Run Code Online (Sandbox Code Playgroud)

像这儿:

{
    "objects": [{
            "id": 1,
            "otherObj": [{
                    "id": 1,
                    ...
                }, {
                    "id": 3,
                    ...
                }]
        },
            "id": 2,
            "otherObj": [{
                    "id": 1,  <-- desired format, whole object
                    ...
                }]
    ]
} …
Run Code Online (Sandbox Code Playgroud)

java json jackson jsonidentityinfo

5
推荐指数
1
解决办法
6371
查看次数

标签 统计

java ×2

hibernate ×1

jackson ×1

jpa ×1

json ×1

jsonidentityinfo ×1

postgresql ×1