Skip to content

Commit

Permalink
Merge pull request eugenp#237 from alex-semenyuk/master
Browse files Browse the repository at this point in the history
Added support common annotation
  • Loading branch information
Eugen committed Aug 21, 2015
2 parents ed2b7e4 + a690614 commit 2245370
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
27 changes: 26 additions & 1 deletion spring-data-mongodb/src/main/java/org/baeldung/model/User.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package org.baeldung.model;

import java.util.Calendar;

import org.baeldung.annotation.CascadeSave;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.index.CompoundIndex;
import org.springframework.data.mongodb.core.index.CompoundIndexes;
import org.springframework.data.mongodb.core.index.IndexDirection;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.DBRef;
Expand All @@ -12,20 +19,34 @@

@QueryEntity
@Document
@CompoundIndexes({ @CompoundIndex(name = "email_age", def = "{'email.id' : 1, 'age': 1}") })
public class User {

@Id
private String id;
@Indexed(direction = IndexDirection.ASCENDING)
private String name;

@Indexed(direction = IndexDirection.ASCENDING)
private Integer age;

@DBRef
@Field("email")
@CascadeSave
private EmailAddress emailAddress;

@Transient
private Integer yearOfBirth;

public User() {
}

@PersistenceConstructor
public User(final String name, @Value("#root.age ?: 0") final Integer age, final EmailAddress emailAddress) {
this.name = name;
this.age = age;
this.emailAddress = emailAddress;
}

public String getId() {
return id;
}
Expand Down Expand Up @@ -57,4 +78,8 @@ public EmailAddress getEmailAddress() {
public void setEmailAddress(EmailAddress emailAddress) {
this.emailAddress = emailAddress;
}

public Integer getYearOfBirth() {
return Calendar.getInstance().get(Calendar.YEAR) - age;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void givenUserExistsWithIndexAddedFromMapping_whenCheckingIndex_thenIndex
public void whenSavingUserWithEmailAddress_thenUserandEmailAddressSaved() {
final User user = new User();
user.setName("Brendan");
EmailAddress emailAddress = new EmailAddress();
final EmailAddress emailAddress = new EmailAddress();
emailAddress.setValue("[email protected]");
user.setEmailAddress(emailAddress);
mongoTemplate.insert(user);
Expand All @@ -172,4 +172,22 @@ public void givenUserExistsWithIndexAddedFromCode_whenCheckingIndex_thenIndexIsE

assertThat(indexInfos.size(), is(2));
}

@Test
public void whenSavingUserWithoutSettingAge_thenAgeIsSetByDefault() {
final User user = new User();
user.setName("Alex");
mongoTemplate.insert(user);

assertThat(mongoTemplate.findOne(Query.query(Criteria.where("name").is("Alex")), User.class).getAge(), is(0));
}

@Test
public void whenSavingUser_thenYearOfBirthIsCalculated() {
final User user = new User();
user.setName("Alex");
mongoTemplate.insert(user);

assertThat(mongoTemplate.findOne(Query.query(Criteria.where("name").is("Alex")), User.class).getYearOfBirth(), is(2015));
}
}

0 comments on commit 2245370

Please sign in to comment.