JPA:实体没有持久化

时间:2014-10-13 18:12:37

标签: java hibernate jpa

考虑一下

@Entity
public class User {
    @Id
    private String id;
    private String firstName;
    private String lastName;
    private String nickName;
    @ElementCollection
    private final List<String> emails = new ArrayList<>();
    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    private final List<Address> addresses = new ArrayList<>();
    @OneToMany
    private final List<Phone> phones = new ArrayList<>();

    public User(@Nonnull final String firstName, @Nonnull final String lastName) {
        id = UUID.randomUUID().toString();
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public User() {
        // required by JPA
    }

    public String getId() {
        return id;
    }

    @Nonnull
    public String getFirstName() {
        return firstName;
    }

    @Nonnull
    public String getLastName() {
        return lastName;
    }

    @Nonnull
    public String getNickName() {
        return nickName;
    }

    public void setNickName(@Nonnull final String nickName) {
        this.nickName = nickName;
    }

    @Nonnull
    public List<String> getEmails() {
        return Collections.unmodifiableList(emails);
    }

    @Nonnull
    public List<Address> getAddresses() {
        return Collections.unmodifiableList(addresses);
    }

    @Nonnull
    public List<Phone> getPhones() {
        return Collections.unmodifiableList(phones);
    }

    public void addAddress(@Nonnull final Address address) {
        addresses.add(address);
    }

    public void addPhone(@Nonnull final Phone phone) {
        phones.add(phone);
    }

    public void addEmail(@Nonnull final String email) {
        emails.add(email);
    }

    // likewise remove address, phones, emails could be added
}

@Entity
public class Address {
    @Id
    private String id;
    private String streetName;
    private String additionalStreetName;
    private String city;
    private String state;
    private int zip;
    private String country;
    private AddressType addressType;
    @ManyToOne
    private User user;

    public Address(@Nonnull final String streetName, @Nullable final String additionalStreetName,
                   @Nonnull final String city, @Nonnull final String state, int zip,
                   @Nonnull final String country) {
        this(streetName, additionalStreetName, city, state, zip, country, AddressType.HOME);
    }

    public Address(@Nonnull final String streetName, @Nullable final String additionalStreetName,
                   @Nonnull final String city, @Nonnull final String state, int zip,
                   @Nonnull final String country, @Nonnull final AddressType addressType) {
        id = UUID.randomUUID().toString();
        this.streetName = streetName;
        this.additionalStreetName = additionalStreetName;
        this.city = city;
        this.state = state;
        this.zip = zip;
        this.country = country;
        this.addressType = addressType;
    }

    public Address() {
        // required by JPA
    }

    @Nonnull
    public String getId() {
        return id;
    }

    @Nonnull
    public String getStreetName() {
        return streetName;
    }

    @Nullable
    public String getAdditionalStreetName() {
        return additionalStreetName;
    }

    @Nonnull
    public String getCity() {
        return city;
    }

    @Nonnull
    public String getState() {
        return state;
    }

    public int getZip() {
        return zip;
    }

    @Nonnull
    public String getCountry() {
        return country;
    }

    @Nonnull
    public AddressType getAddressType() {
        return addressType;
    }

    @Override
    public String toString() {
        return "Address{" +
                "id='" + id + '\'' +
                ", streetName='" + streetName + '\'' +
                ", additionalStreetName='" + additionalStreetName + '\'' +
                ", city='" + city + '\'' +
                ", state='" + state + '\'' +
                ", zip=" + zip +
                ", country='" + country + '\'' +
                ", addressType=" + addressType +
                '}';
    }

    public User getUser() {
        return user;
    }

    public void setUser(final User user) {
        this.user = user;
    }
}

当我运行我的测试时

@Test
public void testAddUserWithAddress() {
    final User user;
    {
        user = new User("Steve", "Jobs");
        Address address1 = new Address("1 Infinite Loop", null, "Cupertino", "California", 94105, "USA");
        Address address2 = new Address("2855 Stevens Creek Blvd", "#1025", "Santa Clara", "California", 95050, "USA");
        user.addAddress(address1);
        user.addAddress(address2);
        crudService.create(user);
        jpaRule.changeTransaction();
    }
    {
        final User userInDb = crudService.find(user.getId(), User.class);
        assertNotNull(userInDb);
        assertEquals("Two address not present", 2, userInDb.getAddresses().size());
        assertEquals(user, userInDb);
    }
}

我回来了

java.lang.AssertionError: Two address not present 
Expected :2
Actual   :0
 <Click to see difference>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotEquals(Assert.java:743)
    at org.junit.Assert.assertEquals(Assert.java:118)
    at org.junit.Assert.assertEquals(Assert.java:555)
    at com.learner.persistence.entities.UserTest.testAddUserWithAddress(UserTest.java:106)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134

我不确定为什么不坚持,我做错了什么?

1 个答案:

答案 0 :(得分:0)

您是否错过了地址与用户之间的链接?

address1.setUser(user);
address2.setUser(user);

你是如何实现crud Service.create(用户)的?