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...

问题:

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:

Note

User

Test

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, CONSTRAINT notes_ibfk_1 FOREIGN KEY (Owner) REFERENCES users (username))

Stacktrace.


回答1:

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.


回答2:

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);
   }
  • 发表于 2019-03-18 06:03
  • 阅读 ( 203 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除