xxm_csdn 2015-11-12 08:55 采纳率: 25%
浏览 1710

springMVC整合EclipseLinkJPA,数据访问问题,急求大神帮忙啊

————————————————————————————
<?xml version='1.0' encoding='UTF-8' ?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!-- 注解支持 -->

<!-- 启动组件扫描,排除@Controller组件,该组件由SpringMVC配置文件扫描 -->
<context:component-scan base-package="com.sdl">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>  
</context:component-scan> 


<!-- glassfish提供数据源 -->
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">  
    <property name="jndiName" value="cems"></property>  
</bean> 
<!-- 用于设置JPA实现厂商的特定属性 -->
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">  
    <property name="database" value="MYSQL"/>
    <property name="showSql" value="true" />  
</bean>   
<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect"/> 
<!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->
<!-- JPA实体管理器工厂 -->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
    <property name="dataSource" ref="dataSource"/>
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>  
    <property name="jpaDialect" ref="jpaDialect"/>
    <property name="persistenceUnitName" value="JinYuAppServerPU"></property>
    <property name="persistenceXmlLocation" value="classpath*:persistence.xml"/>
    <property name="loadTimeWeaver">
        <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/>
    </property>
    <!-- persistence.xml集成-->
    <!-- persistence.xml中实体扫描-->
    <property name="packagesToScan" value="com.sdl.entity"/>
    <!--persistence.xml中的框架属性  -->
    <property name="jpaProperties">
        <props>
            <prop key = "eclipselink.orm.throw.exceptions">true</prop>
            <prop key = "eclipselink.ddl-generation">none</prop>
            <prop key = "eclipselink.ddl-generation.output-mode">database</prop>
            <prop key = "eclipselink.weaving">false</prop>
        </props>
    </property>
    <!-- persistence.xml集成结束-->
</bean>

<!-- 事务管理器 -->  
<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
    <property name="entityManagerFactory" ref="entityManagerFactory" />  
</bean>
<!-- 注解式事务 --> 
<tx:annotation-driven transaction-manager="txManager"/> 


————————————————————————————
@Entity
@Table(name = "bas_account")

public class BasAccount implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Lob
@Column(name = "ID")
private byte[] id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 32)
@Column(name = "Account")
private String account;
@Size(max = 32)
@Column(name = "UserName")
private String userName;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 64)
@Column(name = "PWD")
private String pwd;
@Lob
@Column(name = "RoleID")
private byte[] roleID;
// @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*@(?:a-z0-9?\.)+a-z0-9?", message="电子邮件无效")//if the field contains email address consider using this annotation to enforce field validation
@Size(max = 64)
@Column(name = "EMail")
private String eMail;
@Size(max = 64)
@Column(name = "PWDQuestion")
private String pWDQuestion;
@Size(max = 16)
@Column(name = "PWDAnswer")
private String pWDAnswer;
@Column(name = "Del")
private Short del;

public BasAccount() {
}

public BasAccount(byte[] id) {
    this.id = id;
}

public BasAccount(byte[] id, String account, String pwd) {
    this.id = id;
    this.account = account;
    this.pwd = pwd;
}

public byte[] getId() {
    return id;
}

public void setId(byte[] id) {
    this.id = id;
}

public String getAccount() {
    return account;
}

public void setAccount(String account) {
    this.account = account;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPwd() {
    return pwd;
}

public void setPwd(String pwd) {
    this.pwd = pwd;
}

public byte[] getRoleID() {
    return roleID;
}

public void setRoleID(byte[] roleID) {
    this.roleID = roleID;
}

public String getEMail() {
    return eMail;
}

public void setEMail(String eMail) {
    this.eMail = eMail;
}

public String getPWDQuestion() {
    return pWDQuestion;
}

public void setPWDQuestion(String pWDQuestion) {
    this.pWDQuestion = pWDQuestion;
}

public String getPWDAnswer() {
    return pWDAnswer;
}

public void setPWDAnswer(String pWDAnswer) {
    this.pWDAnswer = pWDAnswer;
}

public Short getDel() {
    return del;
}

public void setDel(Short del) {
    this.del = del;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof BasAccount)) {
        return false;
    }
    BasAccount other = (BasAccount) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.sdl.entity.BasAccount[ id=" + id + " ]";
}

}

————————————————————————————

@Repository
public class BasAccountFacade extends AbstractFacade {

@PersistenceContext(unitName = "JinYuAppServerPU")
private EntityManager em;

@Override
protected EntityManager getEntityManager() {
    return em;
}

public BasAccountFacade() {
    super(BasAccount.class);
}

@Override
@Transactional
public BasAccount find(Object id) {
    Query q = em.createNativeQuery("select * from bas_account where Account = ?1");
    q.setParameter(1, id);
    BasAccount ba = null;
    if (q.getSingleResult() != null) {
        ba = (BasAccount) q.getSingleResult();
    }
    return ba;

}

}
————————————————————————————
@Service
public class BasAccountService {

@Resource
private BasAccountFacade basAccountFacade;

private BasAccount ba;

public void setBasAccountFacade(BasAccountFacade basAccountFacade) {
    this.basAccountFacade = basAccountFacade;
}

public BasAccountFacade getBasAccountFacade() {
    return basAccountFacade;
}

public BasAccount getBa() {
    return ba;
}

public void setBa(BasAccount ba) {
    this.ba = ba;
}


public int login(String name, String pwd) {
    ba = basAccountFacade.find(name);
    if (name == ba.getAccount() && pwd == ba.getPwd()) {
        return 1;
    }
    return 0;
}

}
————————————————————————————
@Controller
@RequestMapping("/basAccount")
public class BasAccountController {
@Resource
private BasAccountFacade baf;

public void setBaf(BasAccountFacade baf) {
    this.baf = baf;
}

@Resource
BasAccountService basAccountService;

public void setBasAccountService(BasAccountService basAccountService) {
    this.basAccountService = basAccountService;
}

@RequestMapping(value = "/login.do/{username}/{pwd}", method = RequestMethod.GET)
@ResponseBody
public Object login(@PathVariable String username, @PathVariable String pwd) {

    int result = basAccountService.login(username, pwd);
    if (result == 1) {
        return basAccountService.getBa();
    } else {
        Map map = new HashMap();
        map.put("msg", "登录失败,请检查用户名和密码是否正确");
        return map;// 登录失败给Android端返回一个错误消息提醒用户
    }
}

@RequestMapping(value = "test")

public String test() {
           return "success";
}

}

————————————————————————————
java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManager.

急求大神帮忙啊,网上查很久了,找不到解决方法;不知道是配置有问题还代码问题?

  • 写回答

1条回答

  • xxm_csdn 2015-11-12 08:59
    关注

    不使用数据库的话,可以跑通,可以确定是JPA的问题,但是不知道怎么回事

    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退