[英]Simple Hibernate Mapping Issue
I'm trying to get a simple mapping done but I am having issues. 我正在尝试完成一个简单的映射,但是遇到了问题。
Basically what I have in Mysql is a User table with just one column that is a varchar(255)
named Username that is the primary key. 基本上,我在Mysql中拥有一个User表,其中只有一列是名为Username的
varchar(255)
,它是主键。
I have one other table called notes which has a primary auto-generating key that is an int
, a date
column, varchar
name and contents columns and a varchar(255)
called owner which should contain a user's username. 我还有另一个表,称为notes,它具有一个主要的自动生成键,该键是
int
, date
列, varchar
名称和内容列以及一个varchar(255)
,该所有者应包含用户的用户名。
This is tagged as a foreign key referencing Users (Username). 这被标记为引用用户(用户名)的外键。
The code to get the session factory is this: 获取会话工厂的代码是这样的:
private static SessionFactory createSessionFactory() {
SessionFactory sessionFactory;
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
configuration.addAnnotatedClass(Note.class);
configuration.addAnnotatedClass(User.class);
StandardServiceRegistry serviceRegistry = new
StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
This works fine without the line that adds the annotated class 'Note' so it is probably an issue with that class. 在没有添加带注释的类“ Note”的行的情况下,此方法可以正常工作,因此该类可能是一个问题。 The error is this:
错误是这样的:
Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister org.hibernate.MappingException 无法获取org.hibernate.persister.entity.SingleTableEntityPersister org.hibernate.MappingException的构造函数
Full stacktrace : 完整的堆栈跟踪 :
Full classes are available here: 完整的课程在这里可用:
UPDATE: fixed Owner/OwnerName variable misnaming however I now get this error: ERROR: 更新:修复了Owner / OwnerName变量的错误命名,但是我现在得到此错误:ERROR:
Cannot add or update a child row: a foreign key constraint fails (
notes
.notes
, CONSTRAINTnotes_ibfk_1
FOREIGN KEY (Owner
) REFERENCESusers
(username
))不能添加或更新子行,外键约束失败(
notes
。notes
,约束notes_ibfk_1
外键(Owner
)参考文献users
(username
))
Stacktrace . Stacktrace 。
The issue is in the Note class. 问题在Note类中。 For the variable owner, setter method name is not proper.
对于变量所有者,setter方法名称不正确。 Instead of
代替
public void setOwnerName(String u) {
this.owner = u;
}
It should be 它应该是
public void setOwner(String u) {
this.owner = u;
}
This should resolve the issue. 这样可以解决问题。
you should first save user then save note in your test class. 您应该先保存用户,然后在测试班级中保存注释。 your code should be like this.
您的代码应该是这样的。
@Test
public void testSave() {
Session session = factory.openSession();
Date date = Date.valueOf(LocalDate.now());
User user = new User("Joseph");
Note note = new Note();
note.setName("Joseph's note");
note.setContents("blah blah blah");
note.setOwnerName("Joseph");
session.beginTransaction();
session.save(user);
session.save(note);
session.getTransaction().commit();
session.close();
System.out.println(date);
}
but with this code, you just have foreign key in database and you don't have relation in your code. 但是使用此代码,您仅在数据库中具有外键,而在代码中没有关系。 your note enttiy can be like below code.
您的注释强度可以像下面的代码。
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "username")
public String getOwner() {
return owner;
}
finally your test class can be like : 最后,您的测试课程可以像:
@Test
public void testSave() {
Session session = factory.openSession();
Date date = Date.valueOf(LocalDate.now());
User user = new User("Joseph");
Note note = new Note();
note.setName("Joseph's note");
note.setContents("blah blah blah");
note.setOwner(user);
session.beginTransaction();
session.save(user);
session.save(note);
session.getTransaction().commit();
session.close();
System.out.println(date);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.