相关疑难解决方法(0)

如何在使用JPA和Hibernate时选择id生成策略

我正在浏览Hibernate参考指南的Id生成部分和"使用Hibernate的java持久性"

Hibernate和JPA结合使用有很多选项.

我正在寻找有关如何选择特定id生成策略的进一步文档.

我也在寻找引爆点.

例如,希望hilo策略可以减少争用.我假设必须有与此选择相关的权衡.

我希望接受有关权衡的教育.

有没有可用的文献?

java hibernate jpa uniqueidentifier id-generation

102
推荐指数
3
解决办法
15万
查看次数

jpa mysql表不存在

我试图将Employee实体插入数据库.我使用JPA,数据库是mysql.当我尝试插入实体时,它给了我test.employee不存在.我假设我没有必要创建表.实体将自动使用带注释的名称创建表.请在下面找到例外和代码.

Exception in thread "main" javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.employee' doesn't exist
Error Code: 1146
Call: INSERT INTO employee (FIRST_NAME, LAST_NAME) VALUES (?, ?)
    bind => [abc,def]
Query: InsertObjectQuery(com.example.entities.Employee@ee1ede)
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:157)
    at com.example.dao.BaseDao.commitAndClose(BaseDao.java:41)
    at com.example..dao.BaseDao.persist(BaseDao.java:29)
    at com.example..dao.EmployeeDao.createEmployee(EmployeeDao.java:29)
    at com.example.Main.main(KITMain.java:27)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.employee' doesn't exist
Error Code: 1146
Call: INSERT INTO employee (FIRST_NAME, LAST_NAME) VALUES (?, ?)
    bind => [ram, …
Run Code Online (Sandbox Code Playgroud)

java mysql hibernate jpa

6
推荐指数
1
解决办法
9574
查看次数

不能在 SpringData 中使用 String 作为 @Id

我想使用这个类在我的数据库上创建新表

@Entity
@Table(name = "currency_rate")
public class CurrencyRate {

    @Id
    private String id;

    @Column(name = "source_currency")
    private String sourceCurrency;

    @Column(name = "target_currency")
    private String targetCurrency;

    @Column(name = "exchange_rate")
    private double exchangeRate;

    @Column
    private Date date;

    @PrePersist
    public void generateID() {            
        this.id = this.date.toString().replace("-", "") + sourceCurrency + targetCurrency;
    }
    //getters, setters
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用属性运行我的应用程序时

spring.jpa.hibernate.ddl-auto=create
Run Code Online (Sandbox Code Playgroud)

我得到了这个例外

引起:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:指定的键太长;最大密钥长度为 1000 字节

好像不能用 Spring 作为我的 ID?将类型更改为 Long 可以解决问题,但我真的很想将 String 与这个一起使用。从我搜索的内容来看,它应该是完全可行的。

mysql spring spring-data

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

x如何使用 Spring Data JPA 在 Spring 中为 CrudRepository 设置 @Id 字符串?

问题是我在使用@RepositoryRestResource我的UserRepository扩展时遇到了异常JpaRepository

原因是默认情况下findById只接受Long或输入Int,即使我有

@Id String id;而不是@Id Int id在我的实体定义中。

我尝试搜索 StackOverflow 和 Google,但没有找到任何解决方案。

错误信息如下:

"Failed to convert from type [java.lang.String] to type [java.lang.Integer] for value '3175433272470683'; nested exception is java.lang.NumberFormatException: For input string: \"3175433272470683\""

我想让它与

@Id String id;

有什么建议?

非常感谢提前。在这里提问是我的荣幸。

实体类:

@Entity // This tells Hibernate to make a table out of this class
@Table(name = "users")
public class XmppUser {
    @Id
    private java.lang.String username;

    private String …
Run Code Online (Sandbox Code Playgroud)

java spring hibernate spring-data-jpa spring-hateoas

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