Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql int到Java长映射到null_Java_Mysql_Hibernate_Jpa - Fatal编程技术网

Mysql int到Java长映射到null

Mysql int到Java长映射到null,java,mysql,hibernate,jpa,Java,Mysql,Hibernate,Jpa,我将JPA与Hibernate一起使用。我有一个@实体,其字段的数据类型已设置为Long。mysql数据库中对应的字段是int(11)。当我试图检索这个字段时,它显示为Null。我错过什么了吗 @Entity @EqualsAndHashCode(of = {"test_id", "other_test_id"}) @Table(name = "test_table") @Data class Dummy{ @Id @Column(name = "test_id", nullab

我将JPA与Hibernate一起使用。我有一个@实体,其字段的数据类型已设置为Long。mysql数据库中对应的字段是int(11)。当我试图检索这个字段时,它显示为Null。我错过什么了吗

@Entity
@EqualsAndHashCode(of = {"test_id", "other_test_id"})
@Table(name = "test_table")
@Data
class Dummy{
    @Id
    @Column(name = "test_id", nullable = false)
    private Long testId;

    @Id
    @Column(name = "other_test_id", nullable = false)
    private Long otherTestId;

}

class DummyDao{

    public Dummy findDummyByOtherTestId(Long otherTestId){

      StringBuffer query = new StringBuffer();
      query.append("SELECT * ");
      query.append("FROM test_table tt WHERE ");

      List<String> criteria = new ArrayList<>();
      criteria.add("tt.other_test_id = :otherTestId");
      query.append(criteria.get(0));

      List<Dummy> results = em.createNativeQuery(query.toString(), Dummy.class).setParameter("otherTestId", otherTestId).getResultList();

      return results.isEmpty() ? null : results.get(0);
    }
}
@实体
@EqualsAndHashCode(of={“test\u id”,“other\u test\u id”})
@表(name=“测试表”)
@资料
类虚拟{
@身份证
@列(name=“test\u id”,null=false)
私人长睾丸;
@身份证
@列(name=“other\u test\u id”,null=false)
私人长睾丸;
}
DummyDao类{
公共虚拟findDummyByOtherTestId(长其他TestId){
StringBuffer查询=新建StringBuffer();
query.append(“SELECT*”);
query.append(“来自test_表tt,其中”);
列表条件=新建ArrayList();
标准。添加(“tt.other_test_id=:otherTestId”);
query.append(criteria.get(0));
List results=em.createNativeQuery(query.toString(),Dummy.class).setParameter(“otherTestId”,otherTestId).getResultList();
返回results.isEmpty()?null:results.get(0);
}
}

所以问题是有多个@Id,我认为这是告诉JPA这个实体有一个复合键的方法

要定义复合键,请执行以下操作:

@Embeddable
public class MyCompositeKey implements Serializable{

    @Column(name = "test_id", nullable = false)
    @Getter
    @Setter
    private Long testId;

    @Column(name = "other_test_id")
    @Getter
    @Setter
    private Long otherTestId;
}


@Entity
@EqualsAndHashCode(of = "compositeKey")
@Table(name = "test_table")
@Data
class Dummy{

    @EmbeddedId
    @Column(name = "test_id", nullable = false)
    private Long compositeKey;
}

一旦我做到了,hibernate使用复合键正确地创建了模式,并且能够检索int字段并映射到Long。

您使用
Long
而不是
Integer
,有什么原因吗?为了避免耗尽空间/在我将id类型更改为更大的数据类型时更改代码为什么要使用
@Id
两个字段上的注释?那个注释应该用在主键字段上。@Matt我实际上是在读这个。我是JPA新手,尝试为具有2列组合键的表创建一个实体。我想我必须遵循是的这样做,你需要创建一个复合键类。也请看下面的例子。