我注意到并非所有的Javascript函数都是构造函数.
var obj = Function.prototype;
console.log(typeof obj === 'function'); //true
obj(); //OK
new obj(); //TypeError: obj is not a constructor
Run Code Online (Sandbox Code Playgroud)
问题1:如何检查函数是否为构造函数,以便可以使用new调用它?
问题2:当我创建一个函数时,是否可以使它不是一个构造函数?
根据其JavaDoc,PersistenceAnnotationBeanPostProcessor似乎负责使用注释@PersistenceContext注入EntityManager.它似乎暗示如果没有在Spring应用程序上下文xml中声明这个bean,@ PerersContext注释将不起作用.
但是,根据我的实验,这不是事实.
<persistence 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"
version="1.0">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL" />
</persistence>
Run Code Online (Sandbox Code Playgroud)
<context:component-scan base-package="com.test.dao" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="default"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect"/>
</bean>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="url" value="jdbc:derby://localhost:1527/c:\derbydb\mydb"/>
<property name="username" value="APP"/>
<property name="password" value="APP"/>
</bean>
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!--
<bean id="persistenceAnnotation" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
-->
Run Code Online (Sandbox Code Playgroud)
@Repository("userDao")
public class UserDaoImpl …Run Code Online (Sandbox Code Playgroud) 我有一个JPA程序,其中EclipseLink是持久性提供程序.当我合并用户实体,更改其ID,并尝试再次合并同一个用户实例时,会引发错误.我重写我的代码以最简单的方式说明我的问题.
User user = userManager.find(1);
userManager.merge(user);
System.out.println("User is managed? "+userManager.contains(user);
user.setId(2);
userManager.merge(user);
Run Code Online (Sandbox Code Playgroud)
上面的代码不在事务上下文中.userManager是一个无状态会话bean,注入了EntityManager.执行时,控制台打印:
User is managed? false
Exception [EclipseLink-7251] (Eclipse Persistence Services - 2.1.3.v20110304-r9073): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The attribute [id] of class [demo.model.User] is mapped to a primary key column in the database. Updates are not allowed.
Run Code Online (Sandbox Code Playgroud)
在第二次merge()调用时发生异常.
如果我创建一个新用户,设置其ID并合并它,它的工作原理如下:
User user = userManager.find(1);
userManager.merge(user);
System.out.println("User is managed? "+userManager.contains(user);
User newUser = new User();
newUser.setId(2);
userManager.merge(newUser);
Run Code Online (Sandbox Code Playgroud)
那么第一个场景和第二个场景之间有什么区别?根据JPA规范,只要实体处于分离状态,合并就应该成功,对吧?(假设存在ID = 2的实体)
为什么EclipseLink提供商似乎对用户实体之前合并的事实感到困扰?
更新:这似乎是EclipseLink的一个错误.我已经将EclipseLink中的持久性提供程序替换为Hibernate:
我改变
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
Run Code Online (Sandbox Code Playgroud)
至
<provider>org.hibernate.ejb.HibernatePersistence</provider>
Run Code Online (Sandbox Code Playgroud)
没有抛出任何错误.