Announcement Announcement Module
No announcement yet.
Generating Unique Exception Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Generating Unique Exception

    I have set up a simple Document called User:
    public class User
    private String username;

    I have set up my UserRepository
    public interface UserRepository extends MongoRepository<User, String>
    User findByUsername(String username);

    I have set up in my applicationContext to generate exceptions
    <mongo:mongo host="localhost" port="27017" id="mongo"/>
    <bean id="mongoTemplate" class=" Template">
    <constructor-arg ref="mongo" />
    <constructor-arg value="testdb" />
    <property name="writeResultChecking" value="EXCEPTION"/>
    <property name="writeConcern" value="SAFE"/>

    Yet, when i insert a second user with same username, while the insert fails, i am not getting an exception. This is using the snapshot build: 1.1.0.BUILD-SNAPSHOT.
    Is this a bug, or am i still missing some configuration?

  • #2
    I see in debugger that the code goes to update when id is already in there. Perhaps the issue is that if you set the id, the Spring layer interprets it as an update. So, when using username as the id we have no choice but to to do a manual check if user exists before the persist is done.
    A good option would be to set this to be insertNew. Alternatively, i can set the username as unique index and not as id.


    • #3

      I just spotted this and am working in this area, so I thought I'd contribute.

      As far as I understand the @Id annotation (or a field called 'id' if no annotations is present), maps to the _id attribute in the mongo document.

      Therefore, if you provide a value, this will be used.

      For example, in using MongoDB for storing my Spring Security user credentials, I have the following:
      	private final String email;
      	private final UUID publicUuid;
      In this case, a different publicUuid will be a different document.

      Where I'm stuck though is that I have a test as follows that fails, and I think should pass (i.e. throw the exception):
      	public void duplicateEmailShouldThrowException() {
      		SimpleUserDetails user1 = new SimpleUserDetails("[email protected]", "password", "Me", "Ape");
      		user1 =;
      		SimpleUserDetails user2 = new SimpleUserDetails("[email protected]", "diffpassword", "NotMe", "NotApe");
      		user2 =;
      		assertNotNull(user2); // should never get here
      I think this is a bug/pending feature...
      Last edited by nealeu; Jun 21st, 2012, 03:22 PM. Reason: formatting


      • #4
        I've looked into this, and it looks like the WriteResult returned from in e.saveDBObject(String, DBObject, Class<?>) is ignored.


        • #5
          I'm looking further at this and drilling deeper, the ignored result should not be important as we should get a MongoException...


          • #6

            The reason the exception is not getting thrown is that the default WriteConcern on MongoTemplate is NONE.

            The following gets me around the problem:

            public class DefaultMongoConfig extends AbstractMongoConfiguration {
            	protected String getDatabaseName() {
            		return "dev-mongodb";
            	public Mongo mongo() throws Exception {
            		return new Mongo("");
            	public MongoTemplate mongoTemplate() throws Exception {
            		MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory(), mappingMongoConverter());
            		return mongoTemplate;