packagesToScan失效问题

wawuyi 发布于 2015/03/08 11:30
阅读 1K+
收藏 0

@飞炀 你好,想跟你请教个问题:

我用hibernat4 spring4,总是找不到实体类,packagesToScan不起作用,必须在hibernate。cfg.xml中添加mapping标签才可以,求助!

applicationContext.xml

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
        <property name="packagesToScan" value="bean"></property>
</bean>

hibernate.cfg.xml

<hibernate-configuration>

    <session-factory name="sessionFactory">
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">admin</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/code1</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <mapping class="bean.UserInfo"/>
    </session-factory>
</hibernate-configuration>

src下bean包中UserInfo实体类

package bean;


// Generated 2015-3-8 10:56:04 by Hibernate Tools 3.4.0.CR1


import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Version;


/**
 * UserInfo generated by hbm2java
 */
@Entity
@Table(name = "user_info", catalog = "code1")
public class UserInfo implements java.io.Serializable {


private Integer id;
private int version;
private String accountId;
private String name;
private Date birthDate;
private int sex;
private Character level;


public UserInfo() {
}


@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "ID", unique = true, nullable = false)
public Integer getId() {
return this.id;
}


public void setId(Integer id) {
this.id = id;
}


@Version
@Column(name = "version", nullable = false)
public int getVersion() {
return this.version;
}


public void setVersion(int version) {
this.version = version;
}


@Column(name = "accountID", nullable = false, length = 20)
public String getAccountId() {
return this.accountId;
}


public void setAccountId(String accountId) {
this.accountId = accountId;
}


@Column(name = "name", nullable = false, length = 15)
public String getName() {
return this.name;
}


public void setName(String name) {
this.name = name;
}


@Temporal(TemporalType.TIMESTAMP)
@Column(name = "birthDate", nullable = false, length = 19)
public Date getBirthDate() {
return this.birthDate;
}


public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}


@Column(name = "sex", nullable = false)
public int getSex() {
return this.sex;
}


public void setSex(int sex) {
this.sex = sex;
}


@Column(name = "level", length = 1)
public Character getLevel() {
return this.level;
}


public void setLevel(Character level) {
this.level = level;
}


}




以下是问题补充:

@wawuyi:<mapping class="bean.UserInfo">是被注释掉的,有了这句就好使了,不懂原因 (2015/03/08 15:17)
加载中
0
w
wawuyi
Configuration cfg = new Configuration().configure();
SessionFactory sessFactory = cfg.buildSessionFactory();
Session sess = sessFactory.openSession();
Transaction tx = sess.beginTransaction();

sess.save(auser);
tx.commit();

sess.close();


操作类写的不对,sessionFactory应该在webAoolication启动时就注入了,但是我自己有重新读了cfig文件,重新根据cfg build一个sessionFactory,导致启动时自动注入的属性失效(包括扫描到的类),所以packagesToScan不是失效,是配置好后,我没有用而是又从新生成了一个sessionFactory。

汗颜。。。

0
w
wawuyi


这么写就对了!

@Autowired
SessionFactory sessionFactory;

/*Configuration cfg = new Configuration().configure();

SessionFactory sessFactory = cfg.buildSessionFactory();
Session sess = sessFactory.openSession();*/
Session sess = sessionFactory.openSession();
Transaction tx = sess.beginTransaction();

sess.save(auser);
tx.commit();
sess.close();
0
飞炀
飞炀
如果只是简单的save,update的话,没必要用openSession方式,只要配置没有错误,sessionFactory.getCurrentSession()就可以了,这样获取的session不需要手动关闭。
返回顶部
顶部