Reputation: 104
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.
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.
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. The error is this:
Could not get constructor for 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:
Cannot add or update a child row: a foreign key constraint fails (
notes
.notes
, CONSTRAINTnotes_ibfk_1
FOREIGN KEY (Owner
) REFERENCESusers
(username
))
Upvotes: 1
Views: 49
Reputation: 136
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);
}
Upvotes: 1
Reputation: 56
The issue is in the Note class. For the variable owner, setter method name is not proper. 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.
Upvotes: 1