使用hibernate注释在View上获得零结果

时间:2016-04-25 13:54:16

标签: java spring hibernate

我正在使用Spring MVC&休眠。我在我的数据库中有一个视图。我已经像桌子一样定义了我的视图。但是我没有@Entity给@Embeddable。但是当我运行下面的代码时,我得到零结果。但该视图上有条目。我在做什么错误?

SubscriptionScheduler.java

package com.span.vms.scheduler;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.span.vms.model.Subscription;
import com.span.vms.service.VulnerabilityService;
public class SubscriptionScheduler {

final static Logger logger = Logger.getLogger(SubscriptionScheduler.class);
@Autowired
VulnerabilityService vulnerabilityService;

public void helloRajesh() {
    List<Subscription> subscriptions = vulnerabilityService.findVulnerabilityBySubscription("rajesh");
    logger.info("Total Subscriptions " + subscriptions.size());
    logger.info("Hello Rajesh " + new Date());
    for (Subscription subscription : subscriptions) {
        logger.info(subscription.getUser() + " - " + subscription.getCve_id());
    }
}

}

VulnerabilityService.java

package com.span.vms.service;
import java.util.List;
import com.span.vms.model.Subscription;
import com.span.vms.model.Vulnerability;
public interface VulnerabilityService {
List<Subscription> findVulnerabilityBySubscription(String string);
}

VulnerabilityServiceImpl.java

package com.span.vms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.span.vms.dao.SubscriptionDao;
import com.span.vms.dao.VulnerabilityDao;
import com.span.vms.model.Subscription;
import com.span.vms.model.Vulnerability;

@Service("vulnerabilityService")
@Transactional
public class VulnerabilityServiceImpl implements VulnerabilityService {


    @Autowired
    private SubscriptionDao subscriptionDao;

    @Override
    public List<Subscription> findVulnerabilityBySubscription(String user) {
        return subscriptionDao.findVulnerabilityBySubscription(user);
    }
}

SubscriptionDao.java

package com.span.vms.dao;

import java.util.List;

import com.span.vms.model.Subscription;

public interface SubscriptionDao {

    List<Subscription> findVulnerabilityBySubscription(String user);
}

SubscriptionDaoImpl.java

package com.span.vms.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.span.vms.model.Subscription;

@Repository("SubscriptionDao")
public class SubscriptionDaoImpl extends AbstractDao<Integer, Subscription> implements SubscriptionDao {

    @SuppressWarnings("unchecked")
    @Override
    public List<Subscription> findVulnerabilityBySubscription(String user) {
        Criteria criteria = createEntityCriteria();
        criteria.add(Restrictions.eq("user", user));
        return (List<Subscription>) criteria.list();
    }
}

AbstractDao.java

package com.span.vms.dao;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

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

    private final Class<T> persistentClass;

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

    @Autowired
    private SessionFactory sessionFactory;

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

    @SuppressWarnings("unchecked")
    public T getByKey(PK key) {
        return (T) getSession().get(persistentClass, key);
    }

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

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

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

}

Subscription.java

package com.span.vms.model;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.Table;

@Embeddable
@Table(name = "subscription")
public class Subscription {
    @Column(name = "cve_id", unique = false, nullable = false)
    private String cve_id;

    @Column(name = "user", nullable = false)
    private String user;

    public String getCve_id() {
        return cve_id;
    }

    public void setCve_id(String cve_id) {
        this.cve_id = cve_id;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

}

1 个答案:

答案 0 :(得分:0)

Embeddable类旨在嵌入具有相同标识的实体中。

https://docs.oracle.com/javaee/6/api/javax/persistence/Embeddable.html

您似乎尝试使用en Embeddable作为实体。