gpt4 book ai didi

Java、 hibernate 、 Spring

转载 作者:搜寻专家 更新时间:2023-10-31 19:50:36 24 4
gpt4 key购买 nike

我有 2 个实体用户和状态。我可以加载用户列表并可以在控制台中看到它的 sql(log)

(select this_.id as id0_0_, this_.cl_point_id as cl2_0_0_, this_.date_ll as date3_0_0_, this_.date_reg as date4_0_0_, this_.name as name0_0_, this_.passw_salt as passw6_0_0_, this_.status_id as status7_0_0_, this_.passw as passw0_0_, this_.login as login0_0_ from users this_)

.但是当我加载 Status 时,列表是空的,控制台中没有 sql(log)。我找不到问题出在哪里?

用户.java

package tj.eskhata.pos.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.*;

@Entity
@Table(name="users")
public class User implements DomainObject {
@Id
private Long id;

@Column(name="name")
private String fullname;

@Column(name="cl_point_id")
private Long clPointId;

@Column(name="login")
private String wiaUsername;

@Column(name="passw")
private String wiaPassword;

@Column(name="status_id")
private Long statusId;

@Column(name="date_reg")
private Date dateReg;

@Column(name="date_ll")
private Date dateLl;

@Column(name="passw_salt")
private String passwSalt;


public User() {
}

public User(String username, String password, String fullname,
boolean isAdmin) {
this.id=Long.valueOf(1);
this.wiaUsername = username;
this.wiaPassword = password;
this.fullname = fullname;
}

public String getFullname() {
return fullname;
}

public String getWiaPassword() {
return wiaPassword;
}

public String getWiaUsername() {
return wiaUsername;
}

public void setFullname(String fullname) {
this.fullname = fullname;
}

public void setWiaPassword(String password) {
this.wiaPassword = password;
}

public void setWiaUsername(String username) {
this.wiaUsername = username;
}

public Long getId() {
return id;
}

public Long getClPointId() {
return clPointId;
}
public Long getStatusId() {
return statusId;
}
public Date getDateReg() {
return dateReg;
}
public Date getDateLl() {
return dateReg;
}
public String getPasswSalt() {
return passwSalt;
}
public void getClPointId(Long clPointId_) {
this.clPointId=clPointId_;
}
public void setStatusId(Long statusId_) {
this.statusId=statusId_;
}
public void setDateReg(Date dateReg_) {
this.dateReg=dateReg_;
}
public void setDateLl(Date dateLl_) {
this.dateLl=dateLl_;
}
public void setPasswSalt(String passwSalt_) {
this.passwSalt=passwSalt_;
}
public boolean isAdmin() {
return false;
}
}

状态.java

package tj.eskhata.pos.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.*;

@Entity
@Table(name="status")
public class Status implements DomainObject {

public Status() {
}
public Status(Long id) {
this.id = id;
}

public Status(Long id, String code, String name, String fullName,
String color) {
this.id = id;
this.code = code;
this.name = name;
this.fullName = fullName;
this.color = color;
}


@Id
private Long id;
public void setId( Long id_) {
this.id=id_;
}
public Long getId() {
return this.id;
}


@Column(name = "code")
private String code;
public void setCode( String code_) {
this.code=code_;
}
public String getCode() {
return this.code;
}

@Column(name = "name")
private String name;
public void setName( String name_) {
this.name=name_;
}
public String getName() {
return this.name;
}

@Column(name = "full_name")
private String fullName;
public void setFullName( String fullName_) {
this.name=fullName_;
}
public String getFullName() {
return this.fullName;
}

@Column(name = "color")
private String color;
public void setColor( String color_) {
this.color=color_;
}
public String getColor() {
return this.color;
}
}

*

UserDaoImpl.java:
package tj.eskhata.pos.dao.hibernate;

import tj.eskhata.pos.dao.UserDao;
import tj.eskhata.pos.domain.User;

public class UserDaoImpl extends AbstractHibernateDaoImpl<User>
implements UserDao {

public UserDaoImpl() {
super(User.class);
}
}

StatusDaoImpl.java:
package tj.eskhata.pos.dao.hibernate;

import tj.eskhata.pos.dao.StatusDao;
import tj.eskhata.pos.domain.Status;

public class StatusDaoImpl extends AbstractHibernateDaoImpl<Status>
implements StatusDao {

public StatusDaoImpl() {
super(Status.class);
}
}

UserDao.java:

package tj.eskhata.pos.dao;

import tj.eskhata.pos.domain.User;

public interface UserDao extends Dao<User> {

}

StatusDao.java:
package tj.eskhata.pos.dao;

import tj.eskhata.pos.domain.Status;

public interface StatusDao extends Dao<Status> {

}

AbstractHibernateDaoImpl.java:

package tj.eskhata.pos.dao.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;

import tj.eskhata.pos.dao.Dao;
import tj.eskhata.pos.domain.DomainObject;

public abstract class AbstractHibernateDaoImpl<T extends DomainObject>
implements Dao<T> {

private Class<T> domainClass;

private SessionFactory sf;

public AbstractHibernateDaoImpl(Class<T> domainClass) {
this.domainClass = domainClass;
}

public SessionFactory getSessionFactory() {
return sf;
}

public void setSessionFactory(SessionFactory sf) {
this.sf = sf;
}

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

@SuppressWarnings("unchecked")
public T load(long id) {
return (T) getSession().get(domainClass, id);
}

public void save(T object) {
getSession().saveOrUpdate(object);
}

@SuppressWarnings("unchecked")
public List<T> findAll() {
Criteria criteria = getSession().createCriteria(domainClass);
List<T> r=(List<T>) criteria.list();
return r;
}

public int countAll() {
Criteria criteria = getSession().createCriteria(domainClass);
criteria.setProjection(Projections.rowCount());
return (Integer) criteria.uniqueResult();
}

public Session getSession() {
// presumes a current session, which we have through the
// OpenSessionInViewFilter; doesn't work without that
return sf.getCurrentSession();
}
}

应用程序上下文.xml:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jndi="http://www.springframework.org/schema/jndi"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/jndi
http://www.springframework.org/schema/jndi/spring-jndi.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.0.xsd">

<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:application.properties</value>
</property>
<property name="systemPropertiesModeName">
<value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
</property>
</bean>

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>${jdbc.driver}</value>
</property>
<property name="jdbcUrl">
<value>${jdbc.url}</value>
</property>
<property name="user">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="minPoolSize">
<value>${c3p0.minPoolSize}</value>
</property>
<property name="maxPoolSize">
<value>${c3p0.maxPoolSize}</value>
</property>
<property name="checkoutTimeout">
<value>20000</value>
</property>
<property name="maxIdleTime">
<value>${c3p0.maxIdleTime}</value>
</property>
<property name="idleConnectionTestPeriod">
<value>${c3p0.idleConnectionTestPeriod}</value>
</property>
<property name="automaticTestTable">
<value>${c3p0.automaticTestTable}</value>
</property>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>tj.eskhata.pos.domain.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
</props>
</property>
</bean>

<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />

<bean id="wicketApplication"
class="tj.eskhata.pos.PosApplication">
</bean>

<bean id="UserDao"
class="tj.eskhata.pos.dao.hibernate.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="CountryDao"
class="tj.eskhata.pos.dao.hibernate.CountryDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="StatusDao"
class="tj.eskhata.pos.dao.hibernate.StatusDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="GenericDao"
class="tj.eskhata.pos.dao.hibernate.GenericDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<bean id="DiscountsService" class="tj.eskhata.pos.services.DiscountsServiceImpl">
<property name="userDao" ref="UserDao" />
<property name="countryDao" ref="CountryDao" />
<property name="statusDao" ref="StatusDao" />
<property name="genericDao" ref="GenericDao" />
</bean>

</beans>

折扣服务:

package tj.eskhata.pos.services;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import tj.eskhata.pos.domain.Country;
import tj.eskhata.pos.domain.Status;
import tj.eskhata.pos.domain.User;

public interface DiscountsService {

<T> T load(Class<T> type, long id);

List<User> findAllUsers();
void saveUser(User user);
void deleteUser(User user);

List<Country> findAllCountries();
void saveCountry(Country country);
void deleteCountry(Country country);

List<Status> findAllStatuses();
void saveStatus(Status status);
void deleteStatus(Status status);
}


package tj.eskhata.pos.services;

import java.util.List;

import tj.eskhata.pos.dao.CountryDao;
import tj.eskhata.pos.dao.GenericDao;
import tj.eskhata.pos.dao.StatusDao;
import tj.eskhata.pos.dao.UserDao;
import tj.eskhata.pos.domain.Country;
import tj.eskhata.pos.domain.Status;
import tj.eskhata.pos.domain.User;

public class DiscountsServiceImpl implements DiscountsService {

private UserDao userDao;
private CountryDao countryDao;
private StatusDao statusDao;
private GenericDao genericDao;

public DiscountsServiceImpl() {
}

public <T> T load(Class<T> type, long id) {
return genericDao.load(type, id);
}

public List<User> findAllUsers() {
return userDao.findAll();
}
public void saveUser(User user) {
userDao.save(user);
}
public void deleteUser(User user) {
userDao.delete(user);
}
public UserDao getUserDao() {
return userDao;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

public List<Country> findAllCountries() {
return countryDao.findAll();
}
public void saveCountry(Country country) {
countryDao.save(country);
}
public void deleteCountry(Country country) {
countryDao.delete(country);
}
public CountryDao getCountryDao() {
return countryDao;
}
public void setCountryDao(CountryDao countryDao) {
this.countryDao = countryDao;
}

public List<Status> findAllStatuses() {
return statusDao.findAll();
}
public void saveStatus(Status status) {
statusDao.save(status);
}
public void deleteStatus(Status status) {
statusDao.delete(status);
}
public StatusDao getStatusDao() {
return statusDao;
}
public void setStatusDao(StatusDao statusDao) {
this.statusDao = statusDao;
}

public GenericDao getGenericDao() {
return genericDao;
}
public void setGenericDao(GenericDao genericDao) {
this.genericDao = genericDao;
}

}

最佳答案

Status 实体看起来不错(它有一个 @Entity 注释,它有一个默认的无参数构造函数,它有一个 @Id), 我无法发现任何明显的东西。

所以我会:

  • 仔细检查启动日志以检查是否有任何投诉。
  • 使用 Hibernate 控制台(如果您使用的是 Eclipse)或任何等效工具,使用原始 HQL 或标准加载Status
  • 无论如何都要写一个单元测试。

哦,顺便说一句,这是不相关的,但你不应该在你的 User 实体中有这样的东西:

@Column(name="status_id")
private Long statusId;

这些 Long 看起来像外键。使用 ORM 时,您应该有对象和对象之间的关联,而不是 id。像这样:

@ManyToOne
private Status status;

clPointId 的注释相同。

恕我直言,您认为“太相关”和“对象不够”。我可能是错的,但在实体中具有外键属性是一个强烈的暗示。


I have changed @Column(name="status_id") private Long statusId; to @ManyToOne private Status status; Now I receiving the ERROR: Error creating bean with name 'sessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on tj.eskhata.pos.domain.User.status references an unknown entity: tj.eskhata.pos.domain.Status

此消息清楚地表明 Status 出现问题,未被识别为实体(因此无法在关联中引用,从而阻止 session 工厂被实例化)。这通常是由于配置或映射问题。所以:

  • 您是否在某处声明了实体(在 hibernate.cfg.xmlpersistence.xml 中)?如果是,您是否声明了 Status?如果您正在使用类路径扫描,是否扫描了 Status
  • 仔细检查映射,检查列名是否确实存在(不清楚您是否使用现有的物理模型)。
  • 开启日志记录(Spring Logging, Hibernate Logging ),这在开发过程中很有用,有助于发现问题。

关于Java、 hibernate 、 Spring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3184069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com