我一直在看你所有关于相关情况的帖子,但没有一篇解决我的情况。
public class TagJpaController implements Serializable {
public TagJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Tag tag) throws PreexistingEntityException, Exception {
if (tag.getSentenceTagList() == null) {
tag.setSentenceTagList(new ArrayList<SentenceTag>());
}
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
List<SentenceTag> attachedSentenceTagList = new ArrayList<SentenceTag>();
for (SentenceTag sentenceTagListSentenceTagToAttach : tag.getSentenceTagList()) {
sentenceTagListSentenceTagToAttach = em.getReference(sentenceTagListSentenceTagToAttach.getClass(), sentenceTagListSentenceTagToAttach.getId());
attachedSentenceTagList.add(sentenceTagListSentenceTagToAttach);
}
tag.setSentenceTagList(attachedSentenceTagList);
em.persist(tag);
for (SentenceTag sentenceTagListSentenceTag : tag.getSentenceTagList()) {
Tag oldTagIdOfSentenceTagListSentenceTag = sentenceTagListSentenceTag.getTagId();
sentenceTagListSentenceTag.setTagId(tag);
sentenceTagListSentenceTag = em.merge(sentenceTagListSentenceTag);
if (oldTagIdOfSentenceTagListSentenceTag != null) {
oldTagIdOfSentenceTagListSentenceTag.getSentenceTagList().remove(sentenceTagListSentenceTag);
oldTagIdOfSentenceTagListSentenceTag = em.merge(oldTagIdOfSentenceTagListSentenceTag);
}
}
em.getTransaction().commit();
} catch (Exception ex) {
if (findTag(tag.getId()) != null) {
throw new PreexistingEntityException("Tag " + tag + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Tag tag) throws IllegalOrphanException, NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Tag persistentTag = em.find(Tag.class, tag.getId());
List<SentenceTag> sentenceTagListOld = persistentTag.getSentenceTagList();
List<SentenceTag> sentenceTagListNew = tag.getSentenceTagList();
List<String> illegalOrphanMessages = null;
for (SentenceTag sentenceTagListOldSentenceTag : sentenceTagListOld) {
if (!sentenceTagListNew.contains(sentenceTagListOldSentenceTag)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain SentenceTag " + sentenceTagListOldSentenceTag + " since its tagId field is not nullable.");
}
}
if (illegalOrphanMessages != null) {
throw new IllegalOrphanException(illegalOrphanMessages);
}
List<SentenceTag> attachedSentenceTagListNew = new ArrayList<SentenceTag>();
for (SentenceTag sentenceTagListNewSentenceTagToAttach : sentenceTagListNew) {
sentenceTagListNewSentenceTagToAttach = em.getReference(sentenceTagListNewSentenceTagToAttach.getClass(), sentenceTagListNewSentenceTagToAttach.getId());
attachedSentenceTagListNew.add(sentenceTagListNewSentenceTagToAttach);
}
sentenceTagListNew = attachedSentenceTagListNew;
tag.setSentenceTagList(sentenceTagListNew);
tag = em.merge(tag);
for (SentenceTag sentenceTagListNewSentenceTag : sentenceTagListNew) {
if (!sentenceTagListOld.contains(sentenceTagListNewSentenceTag)) {
Tag oldTagIdOfSentenceTagListNewSentenceTag = sentenceTagListNewSentenceTag.getTagId();
sentenceTagListNewSentenceTag.setTagId(tag);
sentenceTagListNewSentenceTag = em.merge(sentenceTagListNewSentenceTag);
if (oldTagIdOfSentenceTagListNewSentenceTag != null && !oldTagIdOfSentenceTagListNewSentenceTag.equals(tag)) {
oldTagIdOfSentenceTagListNewSentenceTag.getSentenceTagList().remove(sentenceTagListNewSentenceTag);
oldTagIdOfSentenceTagListNewSentenceTag = em.merge(oldTagIdOfSentenceTagListNewSentenceTag);
}
}
}
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
String id = tag.getId();
if (findTag(id) == null) {
throw new NonexistentEntityException("The tag with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Tag tag;
try {
tag = em.getReference(Tag.class, id);
tag.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The tag with id " + id + " no longer exists.", enfe);
}
List<String> illegalOrphanMessages = null;
List<SentenceTag> sentenceTagListOrphanCheck = tag.getSentenceTagList();
for (SentenceTag sentenceTagListOrphanCheckSentenceTag : sentenceTagListOrphanCheck) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("This Tag (" + tag + ") cannot be destroyed since the SentenceTag " + sentenceTagListOrphanCheckSentenceTag + " in its sentenceTagList field has a non-nullable tagId field.");
}
if (illegalOrphanMessages != null) {
throw new IllegalOrphanException(illegalOrphanMessages);
}
em.remove(tag);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Tag> findTagEntities() {
return findTagEntities(true, -1, -1);
}
public List<Tag> findTagEntities(int maxResults, int firstResult) {
return findTagEntities(false, maxResults, firstResult);
}
private List<Tag> findTagEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Tag.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Tag findTag(String id) {
EntityManager em = getEntityManager();
try {
return em.find(Tag.class, id);
} finally {
em.close();
}
}
public int getTagCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Tag> rt = cq.from(Tag.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
public Tag findTagByName(String name) {
EntityManager em = getEntityManager();
Tag result = null;
try {
TypedQuery<Tag> consultaTag = em.createNamedQuery("Tag.findByName", Tag.class);
consultaTag.setParameter("name", name);
result = consultaTag.getSingleResult();
} finally {
em.close();
}
return result;
}
public Tag findTagByNameAndCreateIfNotExist(String name) {
Tag tag = findTagByName(name);
if(tag == null) {
try {
tag = new Tag();
tag.setId(UUID.randomUUID().toString());
tag.setName(name);
tag.setCreated(new Date());
tag.setModified(new Date());
this.create(tag);
} catch (Exception ex) {
tag = null;
}
}
return tag;
}
@Entity
@Table(name = "tags")
@NamedQueries({
@NamedQuery(name = "Tag.findAll", query = "SELECT t FROM Tag t"),
@NamedQuery(name = "Tag.findById", query = "SELECT t FROM Tag t WHERE t.id = :id"),
@NamedQuery(name = "Tag.findByName", query = "SELECT t FROM Tag t WHERE t.name = :name"),
@NamedQuery(name = "Tag.findByCreated", query = "SELECT t FROM Tag t WHERE t.created = :created"),
@NamedQuery(name = "Tag.findByModified", query = "SELECT t FROM Tag t WHERE t.modified = :modified")})
public class Tag implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "id")
private String id;
@Basic(optional = false)
@Column(name = "name")
private String name;
@Column(name = "created")
@Temporal(TemporalType.TIMESTAMP)
private Date created;
@Column(name = "modified")
@Temporal(TemporalType.TIMESTAMP)
private Date modified;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "tagId")
private List<SentenceTag> sentenceTagList;
public Tag() {
}
public Tag(String id) {
this.id = id;
}
public Tag(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Date getModified() {
return modified;
}
public void setModified(Date modified) {
this.modified = modified;
}
public List<SentenceTag> getSentenceTagList() {
return sentenceTagList;
}
public void setSentenceTagList(List<SentenceTag> sentenceTagList) {
this.sentenceTagList = sentenceTagList;
}
@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 Tag)) {
return false;
}
Tag other = (Tag) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
}
public class Facade {
private TagJpaController tagController;
private EntityManagerFactory emFactory =
Persistence.createEntityManagerFactory("mydatabank");
private Facade() {
tagController = new TagJpaController(emFactory);
}
public static Facade getInstance() {
return FacadeHolder.INSTANCE;
}
private static class FacadeHolder {
private static final Facade INSTANCE = new Facade();
}
public Tag findTagByName(String name) {
return tagController.findTagByName(name);
}
public Tag findTagByNameAndCreateIfNotExist(String name) {
return tagController.findTagByNameAndCreateIfNotExist(name);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="mydatabank" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.data.Charge</class>
<class>com.data.Headquarter</class>
<class>com.data.Judge</class>
<class>com.data.JudgeCharge</class>
<class>com.data.Matter</class>
<class>com.data.Process</class>
<class>com.data.Sentence</class>
<class>com.data.SentenceImportance</class>
<class>com.data.SentenceJudge</class>
<class>com.data.SentenceMatter</class>
<class>com.data.SentenceTag</class>
<class>com.data.SentenceTrack</class>
<class>com.data.SentenceType</class>
<class>com.data.Tag</class>
<class>com.data.Track</class>
<shared-cache-mode>ALL</shared-cache-mode>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mydatabank?zeroDateTimeBehavior=CONVERT_TO_NULL"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="password"/>
</properties>
</persistence-unit>
</persistence>
当我调用findTagByName时,会引发异常。你能帮我吗?
NamedQueries必须在orm.xml文件或实体类中定义。在您的情况下,必须将实体标记定义为
@Entity
@NamedQueries({
@NamedQuery(
name = "Tag.findTagByName",
query = "SELECT t FROM Tag AS t WHERE t.name = :name")
})
public class Tag {
// Your code here
}
一旦定义了命名查询,EntityManager.GetNamedQuery()就可以访问它。
问题内容: 在Jenkins中,我使用参数化的构建。我将“名称”设置为SVN_TAG,使用了“此构建已参数化”和“列出子版本标签”。然后,在“设置内部名称”中将此SVN_TAG设置为“ $ {SVN_TAG}”。但是,该版本然后显示“’$ {SVN_TAG}中的无法识别的宏’SVN_TAG’””,并且它没有设置版本名称。关于为什么构建找不到该宏的任何想法? 问题答案: 如果单击“构建名称”字段旁边
无法自动连接。找不到“CustomAuthenticationSuccessHandler”类型的bean
登录·Instagram点击..!
我是spring的新手,现在我正在尝试使用应用程序上下文执行I18N过程。我正在从事一个maven模块化项目。我的项目包括以下子模块: 模型myApp服务myApp webApp myApp 在services myApp resources文件夹中,我用消息源声明了应用程序上下文 在webApp中,我有I18N所需的文件 webApp MyApp src/main/webApp/WEB-INF/
我正在使用Spring boot应用程序向Kafka主题发送JSON数据数组,但得到以下错误: 配置文件和服务文件代码: 发布JSON: [{“学生ID”:“Q45678123”,“名字”:“ABC”,“名字”:“XYZ”,“年龄”:“12”,“地址”:{“公寓”:“公寓123”,“街道”:“街道信息”,“州”:“州”,“城市”:“城市”,“邮编”:“12345”}},{“学生ID”:“Q4567