相关疑难解决方法(0)

Hibernate不尊重MySQL auto_increment主键字段

我正在努力学习Hibernate的工作原理,并且我遇到了几乎无法接受的学习曲线.我看不出如何让Hibernate尊重我的对象的auto_increment策略.相反,它使用现有ID覆盖数据库中的条目,从1开始.

我有一个简单的Foo对象,由如下定义的MySQL表支持:

CREATE TABLE `Foo` (
  `fooId` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`fooId`),
)
Run Code Online (Sandbox Code Playgroud)

我已经确认用SQL(insert into Foo values();)手动插入多个Foo对象是正确的.

我的Java类使用如下注释指定了ID:

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="fooId")
private Integer id;
Run Code Online (Sandbox Code Playgroud)

然后我执行一些测试代码,简单地实例化Foo对象并将它们保存到数据库中(使用session.save(obj)).它似乎使用自己的主键序列,从一开始,并没有查看表的关键策略.它会覆盖那里的一切.

我已尝试过该@GeneratedValue位的变体(使用所有可能的策略,不考虑括号子句).有人甚至建议GeneratedValue完全放弃.似乎没什么用.

我要留下什么了?我错过了什么?Hibernate真的很难吗?

(如果有人有另一种Java数据库持久性选项,请建议一个.我正在制作原型,而不是长期的mondo工程项目.)

java mysql hibernate

20
推荐指数
2
解决办法
3万
查看次数

错误:对于 JPA 中的 GeneratedValue,给定的 id 不能为 null

我的对象:

@Entity
@Table(name="user")
public class User {
    @Id
    @Column(name="uid")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

  //more code
 }
Run Code Online (Sandbox Code Playgroud)

当我POST user JSON没有时uid,我收到错误,因为给定的 id 不能为 null。这不应该是这种情况,而uid应该由数据库生成。请指出我遗漏了什么。

JSON:

{
"email": "john@mail.com",
"name": "John Doe",
"phone": "98-765-4445"
}
Run Code Online (Sandbox Code Playgroud)

错误:

{
"timestamp": 1501058952038,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.dao.InvalidDataAccessApiUsageException",
"message": "The given id must not be null!; nested exception is java.lang.IllegalArgumentException: The given id must not be null!",
"path": "/api/user/"
}
Run Code Online (Sandbox Code Playgroud)

java rest jpa spring-boot spring-rest

4
推荐指数
1
解决办法
3万
查看次数

hibernate不会在mysql表上生成自动增量约束

我一直在通过不同的论坛搜索问题,并尝试了不同的解决方案,但我仍然无法找到任何正确的答案我的问题.

我正在使用hibernate4注释来映射我的实体.一切正常,但在mysql中使用hibernate创建表时,只检测不到自动增量键.

我有以下代码:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique = true, nullable = false)
private int responseId;
Run Code Online (Sandbox Code Playgroud)

我也试过了

@Id
@GenericGenerator(name="generator", strategy="increment")
@GeneratedValue(generator="generator")
private int responseId;
Run Code Online (Sandbox Code Playgroud)

使用hibernate它可以正常工作,id会自动分配给row,但在mysql表中它没有AutoIncrement Constraint.我必须手动将字段标记为AI.当我手动插入记录以进行测试或使用表的jdbc语句时,这会成为问题.Plz让我知道我在配置中缺少的是阻止hibernate在各个列上施加AutoIncrement Contraint.

mysql hibernate auto-increment hibernate-annotations

3
推荐指数
1
解决办法
6796
查看次数

如何在没有JoinTables的JPA中创建一对多关系?

我正在尝试使用以下表结构在JPA中创建@OneToMany关系.

+----------------+
| CATALOG        |
+----------------+
| catalogId : PK |
| name           |
+----------------+
      |
+----------------+
| PRODUCT        |
+----------------+
| productId : PK |
| name           |
| catalogId : FK |
+----------------+
Run Code Online (Sandbox Code Playgroud)

我已经将类定义为

@Entity
public class Catalog {
    @Id
    @GeneratedValue
    long catalogId;

    @OneToMany(cascade = CascadeType.ALL, 
               orphanRemoval = true, fetch = FetchType.LAZY)
    @JoinColumn(name = "catalogId", 
               nullable = false, insertable = true, updatable = true)
    Set<Product> products = new HashSet<>();

    String name;
}

@Entity
public class Product {
    @Id …
Run Code Online (Sandbox Code Playgroud)

java jpa one-to-many

3
推荐指数
1
解决办法
4341
查看次数

模式验证:缺少表[hibernate_sequences]

我使用Spring 4.3.3.RELEASE,Hibernate 5.2.2.Final,数据库是MySQL.我想尝试策略= GenerationType.TABLE.据我所知,GenerationType.SEQUENCE 我需要数据库中的序列来生成id.

这是我的实体.

@Entity(name = CommentTable.TABLE_NAME)
public class Comment {

    private Integer id;
    private String title;
    private String message;
    private LocalDateTime createdDateTime;
    private Issue issue;


   public Comment() {
   }

   @Id
   @GeneratedValue(strategy = GenerationType.TABLE)
   @Column(name = CommentTable.COLUMN_ID, unique = true, nullable = false)
   public Integer getId() {
       return id;
   }

   public void setId(Integer id) {
       this.id = id;
   }
//Other getters and setters.
}
Run Code Online (Sandbox Code Playgroud)

Spring注释配置

@Configuration
@ComponentScan("com.ita.training.otm") 
@ImportResource("classpath:/config.xml") // XML with DataSource bean
@EnableTransactionManagement
public …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate jpa

2
推荐指数
1
解决办法
2万
查看次数