Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Database \ Spring Data MongoDB combat     - How to configure Proftpd server on Fedora 22 (Server)

- Docker use Dockerfile created since the launch of the service support SSH container mirror (Server)

- AngularJS - Custom instructions (Programming)

- MySQL full-index scan bug (Database)

- CentOS 6 Install Linux kernel source (Linux)

- How to modify the Sublime in Tab four spaces (Linux)

- Loop arrays in C language is not easy to find a bug (Programming)

- in no backup ex: error: Xin Tourou ah backup child process has Singles DAT / US person / bin / in no backup ex (Database)

- Linux (Ubuntu) How iptables port mapping (Server)

- Ubuntu and Derivative Edition users install LMMS 0.4.15 (Linux)

- Try to use Lets Encrypt (Linux)

- Linux systems use IP masquerading anti-hacker (Linux)

- Linux rights management (Linux)

- VMware difference in three network connection (Linux)

- Upgrading Oracle 11.2.0.1 to 11.2.0.3 (Database)

- Ubuntu uses under KVM + Qemu virtual machine build (Linux)

- iOS in the event delivery and the responder chain (Programming)

- OpenSUSE 13.1 OpenSUSE 12.3 and how to install Cinnamon 2.2 Desktop (Linux)

- Java thread pool: ExecutorService, Executors (Programming)

- Eclipse Android development environment installation (Linux)

 
         
  Spring Data MongoDB combat
     
  Add Date : 2018-11-21      
         
         
         
  This article will show in detail how to access Spring Data MongoDB MongoDB database. MongoDB is an open source document type NoSQL database, MongoDB is one of the Spring Data Spring Data modules dedicated to accessing MongoDB database. Spring Data MongoDB module provides both a query based on the method name, and also provides a query-based annotation.

1, with the Spring Data MongoDB configuration and management

To install the MongoDB database, can be downloaded from here: https: //www.mongodb.org/downloads
The installation process will be omitted. After completing MongoDB installed and running, you can start the application development.
First, create a simple Maven project in Eclipse, manage and configure the pom.xml dependency Spring Data MongoDB project.
pom.xml

< Project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
    xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    < ModelVersion> 4.0.0 < / modelVersion>
    < GroupId> SpringDataMongoDBDemo < / groupId>
    < ArtifactId> SpringDataMongoDBDemo < / artifactId>
    < Version> 0.0.1-SNAPSHOT < / version>
    < Dependencies>
        < Dependency>
            < GroupId> org.springframework.data < / ​​groupId>
            < ArtifactId> spring-data-mongodb < / artifactId>
            < Version> 1.7.2.RELEASE < / version>
        < / Dependency>
    < / Dependencies>
< / Project>

Eclipse will download the JAR package and the dependencies to the classpath configuration items. Now the project has been completed dependencies imported, you can start writing the actual code.
You need to first create a persistent entity classes MongoDB database.


Person.java

package com.ch.jpa.entity;

import java.util.ArrayList;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;

@Document (Collection = "person")
public class Person {

    @Id
    private Long personId;

    private String name;

    private int age;

    @DBRef (Db = "address")
    private List < Address > addresses = new ArrayList <> ();

    public Person () {
    }

    @PersistenceConstructor
    public Person (Long personId, String name, int age) {
        super ();
        this.personId = personId;
        this.name = name;
        this.age = age;
    }

    public Long getPersonId () {
        return personId;
    }

    public void setPersonId (Long personId) {
        this.personId = personId;
    }

    public String getName () {
        return name;
    }

    public void setName (String name) {
        this.name = name;
    }

    public int getAge () {
        return age;
    }

    public void setAge (int age) {
        this.age = age;
    }

    public List < Address > getAddresses () {
        return addresses;
    }

    public void setAddresses (List < Address > addresses) {
        this.addresses = addresses;
    }

    @Override
    public String toString () {
        return "Person [personId =" + personId + ", name =" + name + ", age =" + age + ", addresses =" + addresses + "]";
    }
}
Note @Document represents persistent data is to be a collection. If the collection name is not specified, then the default class name is used as a collective name for the entity type.
Note @Id represents annotated field is mapped to the collection _id columns. If the entity class is not used in this comment, then the default domain name id will be mapped to the collection _id columns. And the value of this field is generated automatically by MongoDB driver package, its value is not available in the POJO.
Note @DBRef used to refer to an existing entity classes in the current entity class. However, in the case of a relational database is different, if we save the current entity, it does not save the associated entity references. Persistence related entity references are separated.
Note @PersistenceConstructor for marking constructor entity created when retrieving data from the MongoDB database server.

Here is the Address entity class associated with:

Address.java

package com.ch.jpa.entity;

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

@Document (Collection = "address")
public class Address {

    @Id
    private long addressId;

    private String address;

    private String city;

    private String state;

    private long zipcode;

    public Address () {

        System.out.println ( "CAlling default cons");
    }

    @PersistenceConstructor
    public Address (long addressId, String address, String city, String state, long zipcode) {
        super ();
        this.addressId = addressId;
        this.address = address;
        this.city = city;
        this.state = state;
        this.zipcode = zipcode;
    }

    public String getAddress () {
        return address;
    }

    public void setAddress (String address) {
        this.address = address;
    }

    public String getCity () {
        return city;
    }

    public void setCity (String city) {
        this.city = city;
    }

    public String getState () {
        return state;
    }

    public void setState (String state) {
        this.state = state;
    }

    public long getZipcode () {
        return zipcode;
    }

    public void setZipcode (long zipcode) {
        this.zipcode = zipcode;
    }

    @Override
    public String toString () {
        return "Address [address =" + address + ", city =" + city + ", state =" + state + ", zipcode =" + zipcode + "]";
    }
}
     
         
         
         
  More:      
 
- Heartbeat (v1, v2, pacemaker) cluster components Overview (Server)
- Java Adapter Mode (Programming)
- Docker study notes (Server)
- To compiler and install MariaDB-10.0.20 under CentOS 6.6 (Database)
- Boot automatically remove Linux operating history records (Linux)
- Linux system security configuration (Linux)
- The correct method to modify the maximum number of concurrent connections Tomcat 7 (Server)
- About AWR More Description (Database)
- MySQL tmpdir parameter modification (Database)
- On Android running ClojureScript (Linux)
- Oracle to start to solve the error ORA-27102 (Database)
- PHP call a Python program (Programming)
- Ubuntu U disk do not have write privileges can only read but not write (Linux)
- Add a custom encryption algorithm in OpenSSL (Linux)
- Redis Linux system installation guide (Database)
- Node.js development environment deployment (Server)
- Linux system versions organize local root password cracking method (Linux)
- FastDFS installation and deployment (Server)
- Print Linux system error codes (Linux)
- Android Studio commonly used shortcuts and how to follow the Eclipse Shortcuts (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.