我有一个实体BaseEntity,一个实体帐户,一个AccountRepository,一个调用存储库的save-method的bean和一个arquillian-test。 运行测试不起作用,我不知道为什么。也许有人可以告诉我我错过了什么。
@EqualsAndHashCode(of = "id")
@MappedSuperclass
@Getter
@Setter
public abstract class BaseEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Temporal(TemporalType.TIMESTAMP)
private Date created;
@Temporal(TemporalType.TIMESTAMP)
private Date updated;
private String entityname;
public BaseEntity() {
this.created = new Date();
this.updated = this.created;
this.entityname = UUID.randomUUID().toString();
}
@PreUpdate
@PrePersist
public void update() {
this.updated = new Date();
}
}
@Entity
@Getter
@Setter
@ToString
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Account extends BaseEntity {
@Embedded
@Valid
@NotNull
private Address address;
@Column(length = 20)
@Size(min = 6, max = 20)
@NotNull
private String telephone;
@Column(unique = true, nullable = false)
@Email
@NotNull
private String email;
@Column(length = 40)
@NotNull
private String passhash;
@Column(length = 5)
@NotNull
private Language preferredLanguage;
@Column(length = 32)
private String passwordResetKey;
@Column(length = 32)
private String emailConfirmationKey;
@NotNull
private AccountStatus status;
@Transactional
@Repository
public interface AccountRepository extends JpaRepository<Account, Long>,
CrudRepository<Account, Long> {
Account findByEmail(String email);
}
@Transactional
public class RegistrationBean {
@Inject
private AccountRepository accountRepository;
public Account register(Account _account) {
System.out.println("Id before saving: " + _account.getId());
Account account = this.accountRepository.save(_account);
System.out.println("Id after saving: " + account.getId());
System.out.println("How many accounts are there after saving? : " +
this.accountRepository.count());
return account;
}
@RunWith(Arquillian.class)
public class RegistrationTest {
@Deployment
public static WebArchive createDeployment() {
WebArchive archive = ShrinkWrap.create(WebArchive.class)
.addPackages(true, "net.company")
.addAsResource("META-INF/beans.xml", "WEB-INF/beans.xml")
.addAsResource("persistence-test.xml", "META-INF/persistence.xml");
System.out.println(archive.toString(true));
return archive;
}
@Inject
private RegistrationBean registrationBean;
@Test
public void testRegisterAndLoadPerson() {
Person person = giveMeAValidPerson();
System.out.println("************ 1 Person: " + person.toString());
person = (Person) this.registrationBean.register(person);
System.out.println("************ 2 Person: " + person.toString());
Account loadedPerson =
(this.registrationBean.getAccountByEmail(person.getEmail()));
System.out.println("*************loadedPerson: " + loadedPerson.toString());
}
}
在META-INF下,我有application.properties,beans.xml和persistence.xml application.properties:
#Database Configuration
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost/testApp
db.username=testApp
db.password=testApp
beans.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<jpa:repositories base-package="net.company.app.repositories" />
<context:property-placeholder location="classpath:application.properties"
ignore-resource-not-found="false"/>
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource">
<property name="driverClass" value="${db.driver}"/>
<property name="jdbcUrl" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
</beans:beans>
的persistence.xml:
<?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="net.company_OurAppPU" transaction-type="RESOURCE_LOCAL">
<class>net.wakeit.agency.entities.Account</class>
<class>net.wakeit.agency.entities.Person</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost
/testAgency"/>
<property name="javax.persistence.jdbc.user" value="testApp"/>
<property name="javax.persistence.jdbc.password" value="testApp"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.archive.autodetection" value="class"/>
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.SunONETransactionManagerLookup"/>
<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>
Id始终保持为null并且findAll在保存后不提供任何帐户。 : - 我没有看到,我错过了什么。 非常感谢帮助!