- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法理解为什么这种关系没有被持久化,并且程序不会正常退出,但在 Eclipse 中继续运行。
下面是我的代码,排除了包名:
主要:
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Date;
import PanelDao;
import Panel;
import Panelbinary;
public class PanelService {
static byte[] file;
/**
* @param args
*/
public static void main(String[] args) {
try {
// TODO Auto-generated method stub
InputStream in = new Object().getClass().getClassLoader().getSystemResourceAsStream("Astrology.zip");
BufferedInputStream bin = new BufferedInputStream(in);
byte[] ba = new byte[1024];
ByteArrayOutputStream bout = new ByteArrayOutputStream();
int n = -1;
while(-1 != (n = bin.read(ba))) {
bout.write(ba, 0, n);
}
file = bout.toByteArray();
System.out.println("Astrology.zip bytes:" + file.length);
if(file.length > 0) {
Panel p = new PanelService().uploadAstrologoyPanel();
System.out.println("panel id=" + p.getId());
System.out.println("panelbinaryid=" + p.getPanelbinaries().get(0).getId());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Panel uploadAstrologoyPanel() {
Panel p = new Panel();
PanelDao pd = new PanelDao();
Date d = new Date();
p.setPanelcategoryid(new BigDecimal(1));//books
p.setPanelbinarytypeid(new BigDecimal(5));//json
p.setDescription("Astrology");
p.setDisplayname("Astrology");
p.setFilterid(new BigDecimal(1));//all devices
p.setLabel("Astrology");
p.setPackagename("com.jio.panel.astrology");
p.setCreated(d);
p.setModified(d);
pd.create(p);
Panelbinary pb = new Panelbinary();
pb.setActive(new BigDecimal(0));
pb.setArtifact(file);
pb.setArtifactsize(new BigDecimal(file.length));
pb.setUserversion("1");
pb.setWidget(new BigDecimal(0));
pb.setCreated(d);
pb.setModified(d);
pb.setIncversion(new BigDecimal(1));
p.addPanelbinary(pb);
//pb.setPanel(p);
p = pd.update(p);
pd.entityManager.close();
return p;
}
}
PanelBinary 实体
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;
import javax.persistence.*;
import org.hibernate.annotations.ForeignKey;
/**
* The persistent class for the PANELBINARY_ database table.
*
*/
@Entity
@Table(
name = "PANELBINARY_",
uniqueConstraints = { @UniqueConstraint(
name = "PB_PID_IV_UK",
columnNames = { "PANELID", "INCVERSION" }),
@UniqueConstraint(
name = "PB_PID_UV_UK",
columnNames = { "PANELID", "USERVERSION" }) })
@SequenceGenerator(
name = "PANELBINARYSEQ",
sequenceName = "PANELBINARYSEQ",
allocationSize = 1,
initialValue = 1)
@SuppressWarnings("serial")
public class Panelbinary implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "PANELBINARYSEQ", strategy = GenerationType.SEQUENCE)
private long id;
@Column(name = "ACTIVE", nullable = false)
private BigDecimal active;
@Lob
@Basic(optional = false, fetch = FetchType.LAZY)
@Column(name = "ARTIFACT", nullable = false) private byte[] artifact;
@Column(name = "ARTIFACTSIZE", nullable = true)
private BigDecimal artifactsize;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATED", nullable = false)
private Date created;
@Lob
private byte[] icon;
private BigDecimal iconsize;
@Column(name = "INCVERSION", nullable = false)
private BigDecimal incversion;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "MODIFIED", nullable = false)
private Date modified;
@Lob
private byte[] thumbnail;
private BigDecimal thumbnailsize;
@Column(name = "USERVERSION", nullable = false, length = 50)
private String userversion;
@Column(name = "WIDGET", nullable = true)
private BigDecimal widget;
//bi-directional many-to-one association to Panel
@ManyToOne(optional = false)
@JoinColumn(name = "PANELID", nullable = false)
@ForeignKey(name = "PB_PID_FK")
private Panel panel;
public Panelbinary() {
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public BigDecimal getActive() {
return this.active;
}
public void setActive(BigDecimal active) {
this.active = active;
}
public byte[] getArtifact() {
return this.artifact;
}
public void setArtifact(byte[] artifact) {
this.artifact = artifact;
}
public BigDecimal getArtifactsize() {
return this.artifactsize;
}
public void setArtifactsize(BigDecimal artifactsize) {
this.artifactsize = artifactsize;
}
public Date getCreated() {
return this.created;
}
public void setCreated(Date created) {
this.created = created;
}
public byte[] getIcon() {
return this.icon;
}
public void setIcon(byte[] icon) {
this.icon = icon;
}
public BigDecimal getIconsize() {
return this.iconsize;
}
public void setIconsize(BigDecimal iconsize) {
this.iconsize = iconsize;
}
public BigDecimal getIncversion() {
return this.incversion;
}
public void setIncversion(BigDecimal incversion) {
this.incversion = incversion;
}
public Date getModified() {
return this.modified;
}
public void setModified(Date modified) {
this.modified = modified;
}
public byte[] getThumbnail() {
return this.thumbnail;
}
public void setThumbnail(byte[] thumbnail) {
this.thumbnail = thumbnail;
}
public BigDecimal getThumbnailsize() {
return this.thumbnailsize;
}
public void setThumbnailsize(BigDecimal thumbnailsize) {
this.thumbnailsize = thumbnailsize;
}
public String getUserversion() {
return this.userversion;
}
public void setUserversion(String userversion) {
this.userversion = userversion;
}
public BigDecimal getWidget() {
return this.widget;
}
public void setWidget(BigDecimal widget) {
this.widget = widget;
}
public Panel getPanel() {
return this.panel;
}
public void setPanel(Panel panel) {
this.panel = panel;
}
}
面板实体
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.*;
/**
* The persistent class for the PANEL_ database table.
*
*/
@Entity
@Table(
name = "PANEL_",
uniqueConstraints = @UniqueConstraint(
name = "P_LABEL_PACKAGENAME_UK",
columnNames = { "LABEL", "PACKAGENAME" }))
@SequenceGenerator(
name = "PANELSEQ",
sequenceName = "PANELSEQ",
allocationSize = 1,
initialValue = 1)
@SuppressWarnings("serial")
public class Panel implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(generator = "PANELSEQ", strategy = GenerationType.SEQUENCE)
private long id;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATED", nullable = false)
private Date created;
@Column(name = "DESCRIPTION", length = 250)
private String description;
@Column(name = "DISPLAYNAME", length = 64)
private String displayname;
private BigDecimal filterid;
private BigDecimal initialstateid;
@Column(name = "LABEL", nullable = false, length = 100)
private String label;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "MODIFIED", nullable = false)
private Date modified;
@Column(name = "PACKAGENAME", nullable = false, length = 250)
private String packagename;
private BigDecimal panelbinarytypeid;
private BigDecimal panelcategoryid;
private BigDecimal panelorder;
//bi-directional many-to-one association to Panelbinary
@OneToMany(mappedBy="panel")
private List<Panelbinary> panelbinaries;
public Panel() {
}
public long getId() {
return this.id;
}
public void setId(long id) {
this.id = id;
}
public Date getCreated() {
return this.created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
public String getDisplayname() {
return this.displayname;
}
public void setDisplayname(String displayname) {
this.displayname = displayname;
}
public BigDecimal getFilterid() {
return this.filterid;
}
public void setFilterid(BigDecimal filterid) {
this.filterid = filterid;
}
public BigDecimal getInitialstateid() {
return this.initialstateid;
}
public void setInitialstateid(BigDecimal initialstateid) {
this.initialstateid = initialstateid;
}
public String getLabel() {
return this.label;
}
public void setLabel(String label) {
this.label = label;
}
public Date getModified() {
return this.modified;
}
public void setModified(Date modified) {
this.modified = modified;
}
public String getPackagename() {
return this.packagename;
}
public void setPackagename(String packagename) {
this.packagename = packagename;
}
public BigDecimal getPanelbinarytypeid() {
return this.panelbinarytypeid;
}
public void setPanelbinarytypeid(BigDecimal panelbinarytypeid) {
this.panelbinarytypeid = panelbinarytypeid;
}
public BigDecimal getPanelcategoryid() {
return this.panelcategoryid;
}
public void setPanelcategoryid(BigDecimal panelcategoryid) {
this.panelcategoryid = panelcategoryid;
}
public BigDecimal getPanelorder() {
return this.panelorder;
}
public void setPanelorder(BigDecimal panelorder) {
this.panelorder = panelorder;
}
public List<Panelbinary> getPanelbinaries() {
if(null == panelbinaries) {
panelbinaries = new ArrayList<Panelbinary>();
}
return this.panelbinaries;
}
public void setPanelbinaries(List<Panelbinary> panelbinaries) {
this.panelbinaries = panelbinaries;
}
public Panelbinary addPanelbinary(Panelbinary panelbinary) {
getPanelbinaries().add(panelbinary);
panelbinary.setPanel(this);
return panelbinary;
}
public Panelbinary removePanelbinary(Panelbinary panelbinary) {
getPanelbinaries().remove(panelbinary);
panelbinary.setPanel(null);
return panelbinary;
}
}
DAO
import Panel;
public class PanelDao extends GenericDaoJpaImpl<Panel, Integer> {
@Override
public Panel create(Panel t) {
try {
entityManager.getTransaction().begin();
super.create(t);
entityManager.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
entityManager.getTransaction().rollback();
}
return t;
}
@Override
public Panel update(Panel t) {
try {
entityManager.getTransaction().begin();
super.update(t);
entityManager.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
entityManager.getTransaction().rollback();
}
return t;
}
@Override
public void delete(Panel t) {
try {
entityManager.getTransaction().begin();
super.delete(t);
entityManager.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
entityManager.getTransaction().rollback();
}
}
}
DAO super 类
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
public class GenericDaoJpaImpl<T, PK extends Serializable>
implements GenericDao<T, PK> {
static {
EntityManagerFactory factory = Persistence
.createEntityManagerFactory("dev2db");
entityManager = factory.createEntityManager();
}
protected Class<T> entityClass;
@PersistenceContext
public static EntityManager entityManager;
public GenericDaoJpaImpl() {
ParameterizedType genericSuperclass = (ParameterizedType) getClass()
.getGenericSuperclass();
this.entityClass = (Class<T>) genericSuperclass
.getActualTypeArguments()[0];
}
@Override
public T create(T t) {
this.entityManager.persist(t);
return t;
}
@Override
public T read(PK id) {
return this.entityManager.find(entityClass, id);
}
@Override
public T update(T t) {
return this.entityManager.merge(t);
}
@Override
public void delete(T t) {
t = this.entityManager.merge(t);
this.entityManager.remove(t);
}
}
super dao接口(interface)
import java.io.Serializable;
public interface GenericDao<T, PK extends Serializable> {
T create(T t);
T read(PK id);
T update(T t);
void delete(T t);
}
持久性.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="dev2db" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="hibernate.hbm2ddl.auto" value="validate" />
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@host:1521:xxxx" />
<property name="javax.persistence.jdbc.user" value="xxxx" />
<property name="javax.persistence.jdbc.password" value="xxxx" />
</properties>
</persistence-unit>
</persistence>
日志:
Astrology.zip bytes:267
Mar 04, 2014 1:01:10 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Mar 04, 2014 1:01:10 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Mar 04, 2014 1:01:10 PM org.hibernate.ejb.HibernatePersistence logDeprecation
WARN: HHH015016: Encountered a deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.
Mar 04, 2014 1:01:10 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
name: dev2db
...]
Mar 04, 2014 1:01:10 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
Mar 04, 2014 1:01:10 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Mar 04, 2014 1:01:10 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Mar 04, 2014 1:01:11 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Mar 04, 2014 1:01:11 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Mar 04, 2014 1:01:11 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [oracle.jdbc.OracleDriver] at URL [jdbc:oracle:thin:@host:port:sid]
Mar 04, 2014 1:01:11 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=prodm6_fix, password=****}
Mar 04, 2014 1:01:11 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
Mar 04, 2014 1:01:11 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
Mar 04, 2014 1:01:11 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
Mar 04, 2014 1:01:11 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
Mar 04, 2014 1:01:12 PM org.hibernate.tool.hbm2ddl.SchemaValidator validate
INFO: HHH000229: Running schema validator
Mar 04, 2014 1:01:12 PM org.hibernate.tool.hbm2ddl.SchemaValidator validate
INFO: HHH000102: Fetching database metadata
Mar 04, 2014 1:01:12 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: PRODM6_FIX.PANELBINARY_
Mar 04, 2014 1:01:12 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [icon, thumbnailsize, modified, id, userversion, artifact, thumbnail, created, iconsize, panelid, active, incversion, artifactsize, widget]
Mar 04, 2014 1:01:12 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: xxxx.PANEL_
Mar 04, 2014 1:01:12 PM org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [panelorder, id, initialstateid, created, panelcategoryid, description, packagename, label, panelbinarytypeid, filterid, modified, displayname]
panel id=13
panelbinaryid=0
我想要得到的是 panel 和 panelbinaryid 都有有效值。我没有看到 panelbinary 被持久保存在数据库中。
即使我关闭实体管理器,插入面板后程序也不会终止。
如果能够通过几行代码和注释轻松添加容器管理的事务,而不是采用全新的方式,那就太好了。
问候,
米滕。
最佳答案
1 - 关于持久性问题,这是因为您的关联由 PanelBinary 管理(由您的属性 @OneToMany(mappedBy="panel")
定义),并且没有向下级联关系。
要使其正常工作,您需要:
用级联属性定义一对多关系
@OneToMany(cascade=ALL, mappedBy="panel")
private List<Panelbinary> panelbinaries;
2 - 要使用注释“轻松”设置事务行为,您应该查看 Spring 文档:
基本上,您需要定义一个 Spring 配置文件来定义数据源、事务管理和启用事务注释。还可以在这里找到教程:
http://techannotation.wordpress.com/2012/05/29/5-minutes-with-jpa-transaction/
3 - 没有关于程序未完成的线索..最后的日志显示它到达了主方法的末尾..
关于java - jpa + hibernate + 独立,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22165971/
我有以下情况要解决,但无法正常工作(尝试了Hibernate和EclipseLink): Table_1: Column_A is Primary Key ... some other
我是 JPA 的新手,但必须在该技术中实现我的项目 我想做的是通过 CriteriaQuery 构建一些数据库查询,但不知道如何将参数列表传递给下面的代码: CriteriaBuilder qb =
我是 JPA 新手,注意到可以通过使用 @Version 注释实体中的字段来使用乐观锁定。我只是好奇,如果之前不存在,持久性提供程序是否会创建一个隐式版本字段。例如网站objectdb状态: "Whe
我有一个 JPA 查询 @Query(value = "SELECT SUM(total_price) FROM ... WHERE ...", nativeQuery = true) 当有匹配的记录
JPA 是否会尝试在已经持久(和非分离)的实体上级联持久化? 为了清楚起见,这是我的情况:我想保留一个新用户: public void addUser(){ //User is an enti
显然,OpenJPA。我也看到提到过 EclipseLink 和 Hibernate,但是在功能上有显着差异吗? 最佳答案 大多数差异来自提供者对 OSGi 的感知程度。例如,您可能需要自己将 Hib
我想将 JPA 用于 micronaut。为此,我使用 io.micronaut.data:micronaut-data-hibernate-jpa:1.0.0.M1 库。每当我运行应用程序并点击端点
我正准备为我的应用实现后端,现在我正在投影数据层。我期待着 Spring 。 最佳答案 Spring Data JPA 不是 JPA 实现。它提供了将数据访问层构建到底层 JPA 顶部的方法。您是否应
假设我有一个表 Item,其中包含一个名为 user_id 的列和一个表 User 以及另一个名为 Superuser 的列: CREATE TABLE Item(id int, user_id in
JPA 2.1 规范说: The entity class must not be final. No methods or persistent instance variables of the
我正在从事一个具有一些不寻常实体关系的项目,我在使用 JPA 时遇到了问题。有两个相关对象;用户,让我们称另一个 X。用户与 X 具有一对多和两个一对一的关系。它基本上看起来像这样 [用户实体] @O
我说的是 JavaEE 中的 JPA。在我读过的一本书中谈到: EntityManager em; em.find(Employee.class, id); “这是实体管理器在数据库中查找实例所需的所
我有 JPA 支持的 Vaadin 应用程序。此应用程序中的组件绑定(bind)到 bean 属性(通过独立的 EL 实现)。一些组件绑定(bind)到外部对象(或其字段),由@OneToOne、@O
是否可以使表中的外键唯一?假设我有实体 A 和 B。 答: @Entity class A extends Serializable { @Id private long id; @OneToOne
我在使用 JPA 时遇到了一点问题。考虑这种情况: 表 A (id_a) | 表 B (id_b, id_a) 我需要的是这样的查询: Select a.*, c.quantity from A as
我有一个由 JPA 管理的实体类,我有一个实体需要在其属性中记录更改。 JPA 是否提供任何方法来处理这种需求? 最佳答案 如果您使用 Hibernate 作为 JPA 提供程序,请查看 Hibern
我想实现以下架构: Table A: a_id (other columns) Table B: b_id (other columns) Table C: c_id (other columns)
我有一个愚蠢的问题。如果能做到的话那就太好了,但我并没有屏住呼吸。 我需要链接到我的 JPA 实体的表中的单个列作为所述 JPA 实体中的集合。有什么方法可以让我单独取回与该实体相关的列,而不必取回整
我有一个 Open JPA 实体,它成功连接了多对多关系。现在我成功地获取了整个表,但我实际上只想要该表中的 ID。我计划稍后调用数据库来重建我需要的实体(根据我的程序流程)。我只需要 ID(或该表中
这是我的一个实体的复合主键。 public class GroupMembershipPK implements Serializable{ private static final long
我是一名优秀的程序员,十分优秀!