Java Hibernate OneToOne异常映射错误?

时间:2016-07-11 11:00:28

标签: java hibernate one-to-one

我有以下课程

@Entity
@Table(name = "seek")
public class Seek implements  Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @NotNull
    @Valid
    @OneToOne(mappedBy="url")
    private Url url;

}

@Entity
@Table(name = "url")
public class Url implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "idSeek", referencedColumnName = "id")
    private Seek seek;

}

我的Url表有idSeek列,我的Seek表没有url表的任何关系列。

我收到的例外是:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'seekDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory dao.hibernate.AbstractDao.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [configuration/HibernateConfig.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Referenced property not a (One|Many)ToOne: model.Url.url in mappedBy of model.Seek.url

我的DAO只是扩展

public abstract class AbstractDao<PK extends Serializable, T> {

private final Class<T> persistentClass;

public AbstractDao() {
    this.persistentClass = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
}

@Autowired
private SessionFactory sessionFactory;

protected Session getSession() {
    return this.sessionFactory.getCurrentSession();
}

public T getByKey(PK key) {
    return (T) this.getSession().get(this.persistentClass, key);
}

public T getByColumn(String column, String value) {
    Criteria criteria = this.getSession().createCriteria(this.persistentClass);
    return (T) criteria.add(Restrictions.eq(column, value)).uniqueResult();
}

public void persist(T entity) {
    this.getSession().persist(entity);
}

public void delete(T entity) {
    this.getSession().delete(entity);
}

protected Criteria createEntityCriteria() {
    return this.getSession().createCriteria(this.persistentClass);
}

protected Query getQuery(String query) {
    return this.getSession().createQuery(query);
}

}

像那样

@Repository("seekDao")
public class SeekDaoImpl extends AbstractDao<Integer, Seek> implements SeekDao {

    @Override
    public Seek get(int id) {
        return super.getByKey(id);
    }

    @Override
    public void save(Seek seek) {
        super.persist(seek);
    }
}

我对Hibernate了解不多,问题出在哪里?

EDITED

我做了它,现在编译但是现在我收到了这个:警告:SQL错误:1048,SQLState:23000 错误:专栏&#39; idSeek&#39;不能为空

我在服务类的方法是:

// my service is @Transactional
@Override
public void saveSeek(Seek seek)
{
    this.seekDao.save(seek);

    this.urldao.save(seek.getUrl());//the url is inside the seek obj
}

所以我需要手动获取插入ID并将其放在我的url obj中? 感谢

2 个答案:

答案 0 :(得分:2)

您的Seek类未指向正确的mappedBy属性。

@Entity
@Table(name = "seek")
public class Seek implements  Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @NotNull
    @Valid
    @OneToOne(mappedBy="seek")
    private Url url;

}

答案 1 :(得分:0)

尝试将@OneToOne(mappedBy="url")更改为@OneToOne(mappedBy="seek")