gpt4 book ai didi

Java-Persist无法插入到SQL表

转载 作者:行者123 更新时间:2023-12-02 10:07:33 24 4
gpt4 key购买 nike

在 JSP 页面中触发如下按钮后,我想插入到 MYSQL 中的表中:

<%="http://localhost:8080/repository/file/view/viewPDF.jsp?count=1&f0="+nd.getNodeid()%>
<%
NodeFacade k= new NodeFacade();
k.insert_url(nd.getNodeid(),
"http://localhost:8080/repository/file/view/viewPDF.jsp?count=1&f0="
+ nd.getNodeid());
%>

这个的功能在NodeFacade.java中:

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.EJB;
import javax.ejb.EJBContext;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class NodeFacade implements NodeFacadeLocal, NodeFacadeRemote {

@PersistenceContext(unitName ="PantonPU")
private EntityManager em;
@Resource
private EJBContext ejbContext;
@EJB
KeyGenFacadeLocal keyGenFacade;
@EJB
UserFacadeLocal userFacade;

@EJB
NodeFacadeLocal UrlLink ;
/**
* message
*/
String msg = "";

/**
* Return messages(if any) after invoking a bean function
* @return (String)
*/
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public String getMsg() {
return msg;
}

/**
* Creates a node { Nodedesc, Nodegid, Nodegdt } must be set. Attributes
* are added from the attribute map Node.attrMap if they are set.
*
* @param n
* @return
* Node with identifier {ID, DT} set on success, null on error
*
*/
public Node create(Node n) {
try {

if (n.getNodedesc() == null || n.getNodegid().longValue() == 0) {
throw new Exception("Either description, or graph space is not set. Aborting...");
}

// allocate a unique identifer value for the node
long id = keyGenFacade.generate("NODE");
if (id == -1) {
throw new Exception(keyGenFacade.getMsg());
}

n.setNodeid(id);
n.setNodedt(new Date());

// checks if the creator node is set:
if (n.getNodeuid() == null) {
Auser u = userFacade.get("root", false);
if (u == null) {
throw new Exception(userFacade.getMsg());
}
n.setNodeuid(BigInteger.valueOf(u.getAuserPK().getNodeid()));
}

// persist the node into the db first
em.persist(n);

// add in the attributes for the node
if (n.get_attrs() != null) {

if (add_attr(n.getNodeid(),
n.get_attrs()) == -1) {
throw new Exception("Unable to add attributes");
}

}

// adds an audit trail
if (add_hist(n.getNodeid(),
NodeHist.NEW,
"Object (" + n.getNodetype() + ") created.",
n.getNodeuid().longValue()) == -1) {
throw new Exception("Unable to add log");
}

return n;
} catch (Exception e) {

ejbContext.setRollbackOnly();

msg = CoreUtil.wrapMsg(CoreUtil.FUNC_ERROR,
this.getClass().getName(), "create", e.getMessage());

return null;
}

}

public void insert_url(long nodeid,String url){
try {
String sql = "INSERT INTO urllink (NODEID, URL) VALUES ("
+ nodeid + ", '" + url + "')";
em.persist(sql);
} catch (Exception e) {
e.printStackTrace();
}
}

没有错误,但它没有显示在我的数据库表中。我做错什么了吗?

尝试对其进行调试,它可以将值正确传递给函数,但在 em 中,它显示为 null。

编辑:

持久性.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="PantonPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/PANTON9</jta-data-source>
<class>com.htasia.panton9.Entities.Core.Apasswd</class>
<class>com.htasia.panton9.Entities.Core.Audel</class>
<class>com.htasia.panton9.Entities.Core.Augroup</class>
<class>com.htasia.panton9.Entities.Core.Auser</class>
<class>com.htasia.panton9.Entities.Core.Graph</class>
<class>com.htasia.panton9.Entities.Core.Locktbl</class>
<class>com.htasia.panton9.Entities.Core.Logging</class>
<class>com.htasia.panton9.Entities.Core.Modlattr</class>
<class>com.htasia.panton9.Entities.Core.Module</class>
<class>com.htasia.panton9.Entities.Core.Nhist</class>
<class>com.htasia.panton9.Entities.Core.Node</class>
<class>com.htasia.panton9.Entities.Core.Nodeattr</class>
<class>com.htasia.panton9.Entities.Core.Nodelink</class>
<class>com.htasia.panton9.Entities.MCRE.UrlLink</class>
<class>com.htasia.panton9.Entities.Core.Seqtbl</class>
<class>com.htasia.panton9.Entities.Core.Uwidget</class>
<class>com.htasia.panton9.Entities.Core.Widget</class>
<class>com.htasia.panton9.Entities.Form.Frxcontainer</class>
<class>com.htasia.panton9.Entities.Form.Frxebinary</class>
<class>com.htasia.panton9.Entities.Form.Frxelement</class>
<class>com.htasia.panton9.Entities.Form.Frxeoptions</class>
<class>com.htasia.panton9.Entities.MCRE.Converr</class>
<class>com.htasia.panton9.Entities.MCRE.Convqueue</class>
<class>com.htasia.panton9.Entities.MCRE.Filesec</class>
<class>com.htasia.panton9.Entities.MCRE.Filext</class>
<class>com.htasia.panton9.Entities.MCRE.Filter</class>
<class>com.htasia.panton9.Entities.MCRE.Fmedia</class>
<class>com.htasia.panton9.Entities.MCRE.Foldersec</class>
<class>com.htasia.panton9.Entities.MCRE.Fverinfo</class>
<class>com.htasia.panton9.Entities.MCRE.Imageinfo</class>
<class>com.htasia.panton9.Entities.MCRE.Pdfinfo</class>
<class>com.htasia.panton9.Entities.MCRE.Pindex</class>
<class>com.htasia.panton9.Entities.MCRE.Referencelink</class>
<class>com.htasia.panton9.Entities.MCRE.Stcknote</class>
<class>com.htasia.panton9.Entities.MCRE.Swfinfo</class>
<class>com.htasia.panton9.Entities.MCRE.Windex</class>
<class>com.htasia.panton9.Entities.Notify.Iprxntfevent</class>
<class>com.htasia.panton9.Entities.Notify.Ntfevent</class>
<class>com.htasia.panton9.Entities.Notify.Ntfobj</class>
<class>com.htasia.panton9.Entities.Notify.Ntfqlog</class>
<class>com.htasia.panton9.Entities.Notify.Ntfqueue</class>
<class>com.htasia.panton9.Entities.Notify.Ntfug</class>
<class>com.htasia.panton9.Entities.Workflow.Hprxattch</class>
<class>com.htasia.panton9.Entities.Workflow.Hprxattchsec</class>
<class>com.htasia.panton9.Entities.Workflow.Hprxendrule</class>
<class>com.htasia.panton9.Entities.Workflow.Hprxlink</class>
<class>com.htasia.panton9.Entities.Workflow.Hprxlog</class>
<class>com.htasia.panton9.Entities.Workflow.Hprxmark</class>
<class>com.htasia.panton9.Entities.Workflow.Hprxprocess</class>
<class>com.htasia.panton9.Entities.Workflow.Hprxrule</class>
<class>com.htasia.panton9.Entities.Workflow.Hprxrulestation</class>
<class>com.htasia.panton9.Entities.Workflow.Hprxstation</class>
<class>com.htasia.panton9.Entities.Workflow.Hprxtask</class>
<class>com.htasia.panton9.Entities.Workflow.Iprxattch</class>
<class>com.htasia.panton9.Entities.Workflow.Iprxendrule</class>
<class>com.htasia.panton9.Entities.Workflow.Iprxlink</class>
<class>com.htasia.panton9.Entities.Workflow.Iprxlog</class>
<class>com.htasia.panton9.Entities.Workflow.Iprxmark</class>
<class>com.htasia.panton9.Entities.Workflow.Iprxprocess</class>
<class>com.htasia.panton9.Entities.Workflow.Iprxrule</class>
<class>com.htasia.panton9.Entities.Workflow.Iprxrulestation</class>
<class>com.htasia.panton9.Entities.Workflow.Iprxstation</class>
<class>com.htasia.panton9.Entities.Workflow.Iprxtask</class>
<class>com.htasia.panton9.Entities.Workflow.Prxaction</class>
<class>com.htasia.panton9.Entities.Workflow.Prxattch</class>
<class>com.htasia.panton9.Entities.Workflow.Prxendrule</class>
<class>com.htasia.panton9.Entities.Workflow.Prxendstatus</class>
<class>com.htasia.panton9.Entities.Workflow.Prxevent</class>
<class>com.htasia.panton9.Entities.Workflow.Prxexception</class>
<class>com.htasia.panton9.Entities.Workflow.Prxlink</class>
<class>com.htasia.panton9.Entities.Workflow.Prxprocess</class>
<class>com.htasia.panton9.Entities.Workflow.Prxrule</class>
<class>com.htasia.panton9.Entities.Workflow.Prxrulestation</class>
<class>com.htasia.panton9.Entities.Workflow.Prxstation</class>
<class>com.htasia.panton9.Entities.Workflow.Prxversion</class>
<class>com.htasia.panton9.Entities.Workflow.Hprxattchsec</class>
<class>com.htasia.panton9.Entities.Workflow.Iprxattchsec</class>
<class>com.htasia.panton9.Entities.Workflow.Prxattchsec</class>
<class>com.htasia.panton9.Entities.Workflow.Prxfrx</class>
<class>com.htasia.panton9.Entities.Workflow.PrxfrxPK</class>
<class>com.htasia.panton9.Entities.Workflow.Prxfrxstn</class>
<class>com.htasia.panton9.Entities.Workflow.Prxfrxstnelm</class>
<class>com.htasia.panton9.Entities.Workflow.PrxfrxstnelmPK</class>
<class>com.htasia.panton9.Entities.Notify.Iprxntfevent</class>
<class>com.htasia.panton9.Entities.Notify.Ntfevent</class>
<class>com.htasia.panton9.Entities.Notify.Ntfobj</class>
<class>com.htasia.panton9.Entities.Notify.Ntfqlog</class>
<class>com.htasia.panton9.Entities.Notify.Ntfqueue</class>
<class>com.htasia.panton9.Entities.Notify.Ntfug</class>
<class>com.htasia.panton9.Entities.MCRE.Fmpolicy</class>
<class>com.htasia.panton9.Entities.Workflow.Prxapprvlist</class>
<class>com.htasia.panton9.Entities.Workflow.Prxdlist</class>
<class>com.htasia.panton9.Entities.Workflow.Prxdlistelem</class>
<class>com.htasia.panton9.Entities.Workflow.Prxdoccon</class>
<class>com.htasia.panton9.Entities.MCRE.Fmediainfo</class>
<class>com.htasia.panton9.Entities.Workflow.Hprxremarks</class>
<class>com.htasia.panton9.Entities.Workflow.Iprxremarks</class>
<class>com.htasia.panton9.Entities.MCRE.Tmpfmpolicy</class>
<class>com.htasia.panton9.Entities.Workflow.Mprxstation</class>
<class>com.htasia.panton9.Entities.MCRE.Erequest</class>
<class>com.htasia.panton9.Entities.MCRE.Ereqdetail</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
</properties>
</persistence-unit>
</persistence>

最佳答案

您能否在代码中创建如下所示的entityManager对象。

EntityManagerFactory ENTITY_MANAGER_FACTORY = Persistence.createEntityManagerFactory("PantonPU");//
EntityManager em = ENTITY_MANAGER_FACTORY.createEntityManager();

关于Java-Persist无法插入到SQL表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55233544/

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