- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个如下所示的测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class)
public class TestTruckRepository {
@Autowired
private TruckRepository truckRepository;
@Transactional
@Test
public void doIt(){
//omitted
}
}
甚至没有到达方法 doIt()。我收到的错误是:
org.hibernate.AnnotationException: Unable to create unique key constraint (PlateNumber) on table Truck: database column 'plateNumber' not found. Make sure that you use the correct column name which depends on the naming strategy in use (it may not be the same as the property name in the entity, especially for relational types)
这是错误所引用的类:
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"plateNumber"}))
public class Truck implements Identifiable {
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.MERGE)
private PlateNumber plateNumber;
private String stuff;
@Id
@GeneratedValue
private Long id;
//Getter setter and constructor are omitted.
}
如果我删除“uniqueConstraints = ....”,那么它似乎工作正常,但该属性不是唯一的。有什么想法如何解决这个问题吗?该错误告诉我查看命名。我真的很想避免将 @Column 或 @JoinColumn (我知道这可以解决它)放在我的所有属性上。我猜测 hibernate 正在创建一个与属性不同的名称,因为它比仅将 String 作为属性更复杂,但我想避免将该信息存储在我的类中。
编辑:所有表都是由 hibernate 创建的,因此我无法进入数据库并仅查找列名。
最佳答案
卡车表中的plateNumber 的列名称是什么(在数据库中检查)?
@UniqueConstraint(columnNames = {"plateNumber"})
UniqueConstraint 具有列名称,因此它不必与您的属性名称相同。您使用什么命名策略? JPA根据配置的命名策略创建表、列和其他对象的名称。默认情况下应该使用 SimpleNamingStrategy,在这种情况下它应该可以工作,因为结果列名称应该是属性名称plateNumber。
关于java - JPA 中自定义类型的 UniqueConstraint 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35869528/
在我的 Spring/hibernate 项目中,我使用 uniqueConstraints = {@UniqueConstraint(columnNames={"ID_A", "ID_B"})} 来
我有一个 Product 实体,它的 name 和 category 组合对于每个产品都应该是唯一的。 在我的例子中,name 是必需的,但 category 可能不存在。 Product 定义如下:
这个问题已经有答案了: What does JPA @UniqueConstraint do in Hibernate? (1 个回答) 已关闭 6 年前。 在以下实体中,我想在数据库中进行唯一的写入
一个快速的 SQLAlchemy 问题... 我有一个类“Document”,其属性为“Number”和“Date”。我需要确保同一年没有重复的数字,是有没有办法在“数字 + 年(日期)”上设置 Un
使用以下模型,为什么以下交互在同一事务中成功地向关系添加重复关联?我预计(并且需要)它会因关联表上放置的 UniqueConstraint 而失败。 模型: from app import db #
我有一个对 3 个字段具有唯一约束的实体。 2 个字段不可为空,第三个字段可为空。 我的问题是,当第三个字段为空时,唯一约束不起作用,因此我的数据库中可以有一些重复的值,例如 (5,1,null)(5
我有一个数据模型,其中许多实体从单个父类(super class)实体继承一些公共(public)属性。我在父类(super class)上使用 InheritanceType.JOINED ,这会导
您能否澄清一下此注释的实际用途? - 如果我们没有使用数据库中的 SQL 表定义定义相应的约束,会发生什么情况。当我们尝试插入时,hibernate 会检查唯一性吗?或者这就是DB的目的吗?如果 hi
在 Spring JPA 中,我有一个实体,并使用 FlywayDb 初始化架构。我的实体是: @Entity @Table(schema = "scheduler", uniqueC
我刚开始在 C# 上使用 db4o,但在设置数据库上的 UniqueConstraint 时遇到了问题。 这是 db4o 配置 static IObjectContainer db = Db4oEmb
我有一个注释如下的实体: @Entity @Table(uniqueConstraints={@UniqueConstraint(columnNames={"name"})}) public clas
我只是想获得有关此导入的解释,我一直在寻找一些好的解释,但找不到。 javax.persistence.UniqueConstraint http://www.mkyong.com/hibernate
我正在为我的 django 应用程序 SrdObject 创建一个抽象模型。我的模型的特征之一是它有一对字段,它们放在一起必须是唯一的:“name”和外键“module”。 这是我的一个例子 clas
几天来,我遇到了一个 Doctrine 实体的小问题。我在两个字段上使用 UniqueConstraint 定义它,然后在这两个字段上使用 UniqueEntity 验证。但是在通过添加一个已经在 b
我正在尝试通过下一种方式在 Flask-SQLAlchemy 的父类中添加唯一约束。如您所见,继承以 Joined Table 方式表示。 class Parametric(object, Model
我想将 UniqueConstraint 与 flask_sqlalchemy 一起使用 不幸的是,这个导入语句: from flask_sqlalchemy import SQLAlchemy, U
校园有@OneToMany 大楼 大楼有@OneToMany 房间。 房间名称在校园内必须是唯一的 (例如校园-A,A座,A室和校园-B,A座,A室应该可以存储) 是否可以在 Room 实体上定义这样
处理一些遗留的 hibernate 代码。 如何使用 hbm.xml( hibernate 映射文件)而不是注释来执行以下操作? @Table(name="users", uniqueConstrai
我有这个代码: @Table(uniqueConstraints = @UniqueConstraint(columnNames = {"name", "color"})) @MappedSuperc
我有一个类使用单表策略(我无法更改)扩展现有实体。我想对该实体使用 UniqueConstraint,所以我尝试了: @Entity @Table(name = "t_document") publi
我是一名优秀的程序员,十分优秀!