2014-10-26 5 views
9

ho fatto un esperimento ... un soggetto comune per i repository di Data due Primavera: - JPA - MongoDBDati di primavera mongodb. Generazione di errore di id

prima di tutto' utilizzando seguenti librerie versioni:

primavera-dati- JPA: 1.7.0.RELEASE primavera-dati-mongodb: 1.6.0.RELEASE

ho un Entity:

@Entity 
@Table(name = "ACCOUNTS") 
public class Account { 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    @Column(name = "ACCOUNT_ID") 
    private Long id; 

    @Column(name = "ACCOUNT_NUMBER") 
    private String number; 

    public Account() { 
    } 

    public Account(String number) { 
     this.number = number; 
    } 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getNumber() { 
     return number; 
    } 

    public void setNumber(String number) { 
     this.number = number; 
    } 
} 

JPA Repos itory ha seguente aspetto:

public interface Repository extends CrudRepository<Account, Long> { 
    public Account findByNumber(String number); 
} 

MongoDB repository ha seguente aspetto:

pacchetto ua.home.springdata.investigation.repository.mongo;

public interface Repository extends CrudRepository<Account, Long> { 
} 

Quindi ... APP funziona :) :) Niente di speciale Ma prova MongoDB non è passato :( sto ottenendo un errore:

 
org.springframework.dao.InvalidDataAccessApiUsageException: Cannot autogenerate id of type java.lang.Long for entity of type ua.home.springdata.investigation.entity.Account! 
    at org.springframework.data.mongodb.core.MongoTemplate.assertUpdateableIdIfNotSet(MongoTemplate.java:1149) 
    at org.springframework.data.mongodb.core.MongoTemplate.doSave(MongoTemplate.java:878) 
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:833) 
    at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:73) 
    at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:88) 
    at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:45) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:442) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:427) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:381) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207) 
    at com.sun.proxy.$Proxy26.save(Unknown Source) 

Penso che sia un caso molto comune . Perché non è la primavera dei dati in grado di generare l'ID entità come a lungo? E 'così strano.

+0

Quando sto aggiungendo l'annotazione ** @ GeneratedValue ** mio IDE sta lanciando un errore allora e là. Ho usato la dipendenza ** jpa ** nel mio file 'pom.xml', ma anche quello non funziona come quello che ho ottenuto è che cerca un DB MySQL e sto usando NoSQL (mongoDB). Quindi se puoi aiutarmi con qualcosa per favore fallo. Grazie in anticipo. – Learner

+0

@Neil Stockton Quando aggiungo l'annotazione ** @ GeneratedValue ** il mio IDE lancia un errore allora e là. Ho usato la dipendenza ** jpa ** nel mio file 'pom.xml', ma anche quello non funziona come quello che ho ottenuto è che cerca un DB MySQL e sto usando NoSQL (mongoDB). Quindi se puoi aiutarmi con qualcosa per favore fallo. Grazie in anticipo .. – Learner

risposta

17

Mongo objectIds non mappare a un tipo lungo java.

vedo questo nel documento mentazione, sotto 7.6.1:

http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo-template.id-handling

An id property or field declared as a String in the Java class will be converted to and stored as an ObjectId if possible using a Spring Converter. Valid conversion rules are delegated to the MongoDB Java driver. If it cannot be converted to an ObjectId, then the value will be stored as a string in the database.

An id property or field declared as BigInteger in the Java class will be converted to and stored as an ObjectId using a Spring Converter.

Quindi modificare l'ID in una stringa o un BigInteger e rimuovere l'argomento strategia.

+0

Quindi perderò la compatibilità con l'implementazione Spring JPA :( – b3lowster

+0

@Robert Moskal Quando aggiungo l'annotazione ** @ GeneratedValue ** il mio IDE lancia un errore allora e lì. Ho usato ** jpa * * dipendenza nel mio file 'pom.xml', ma anche quello non funziona come ho ottenuto è che cerca un DB MySQL e sto usando NoSQL (mongoDB). Quindi se mi può aiutare con qualcosa per favore. Grazie in anticipo – Learner

+0

Aggiungi una nuova domanda e taggami –

1

L'utilizzo di @Id come una stringa funziona correttamente.

Assicurarsi che il repository si estende con una stringa (stesso tipo della @Id):

extends MongoRepository<MyEntity, String> 
1

ho provato qualcosa di simile anche per Mongo db ho dovuto usare la versione import org.springframework.data.annotation.Id; di @Id mentre JPA Ho usato import javax.persistence.Id;

1

Penso che il problema è che stai usando "Entità" invece di "Documento". I Mongo dao dovrebbero usare l'annotazione "Document" e il repository dovrebbe estendere l'interfaccia "MongoRepository". Questo sarebbe un esempio usando quello che hai. In primo luogo si vorrà aggiungere la dipendenza mongo al vostro pom (suppongo che si sta utilizzando genitore avvio primavera, quindi il numero di versione sarà definito ci)

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-mongodb</artifactId> 
</dependency> 

import org.springframework.data.annotation.Id; 
import org.springframework.data.mongodb.core.mapping.Document; 

@Document(collection = "ACCOUNTS") 
public class Account { 

     @Id 
     private String id; 

     ....rest of properties 
} 

import org.springframework.data.mongodb.repository.MongoRepository; 
public interface AccountRepository extends MongoRepository<Account, String> { 
     //any extra queries needed 
} 
0

Il mio progetto utilizzando primavera dati Resto + mongo

  1. Tipo di dati Non sto utilizzando alcun tipo di Long o BigInteger. È un oggetto personalizzato. Diciamo CompanyUID.class. Qui deve essere MongoRepository<DataLoadMerchant, CompanyUID> come dice @Miguel Poi ho cambiato il mio getter e setter.Conversione di stringhe in CompanyUID o CompanyUID per String

  2. convertitore registro in Mongo

    @Configuration 
    public class MongoConfig extends AbstractMongoConfiguration { 
    @Override 
    public CustomConversions customConversions() { 
        converters.add(new CompanyUIDoObjectIdConverter()); 
        converters.add(new ObjectIdToCompanyUIDConverter()); 
        return new CustomConversions(converters); 
        } 
    } 
    
  3. nome della colonna. Guardo il documento mongo. sembra che non possa avere un ID entità con @ ID e anche utilizzare entityId come nome della colonna. Così cambio setter Quindi in MongoDB avrà 2 colonne Uno è _id e l'altro è entityId. Le due colonne mantengono lo stesso valore. E usiamo solo EntityID come chiave primaria per le operazioni CRUD, anche se non è la vera chiave primaria

mio codice

public class ClassA implements Serializable { 
    @Id 
    public CompanyUID id; 
    public CompanyUID entityId;' 

    public String getId() { 
     return this.id.toString(); 
    } 

    public void setId(String id) { 
     if (id != null && this.entityId != null) { 
      if (!id.equals(this.entityId)) { 
       throw new Exception(); 
      } 
     } 
     this.id = new CompanyUID(id); 
     this.entityId = this.id; 
    } 

    public String getEntityId() { 
     return entityId.toString(); 
    } 

    public void setEntityId(String entityId) { 
     if (this.id != null && entityId != null) { 
      if (!id.equals(entityId)) { 
       throw new Exception(); 
      } 
     } 

     this.entityId = new CompanyUID(entityId); 
     this.id = this.entityId; 
     } 
    } 
Problemi correlati