Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Database \ Spring Data MongoDB combat     - Ten best plug surge Emacs Productivity (Linux)

- Do not find ifconfig eth0 and IP address under CentOS6.5 (Linux)

- RedHat Linux source code package installation process (Linux)

- Single list summarizes the basic operation (Programming)

- Android and OpenCV2.4.4 build an image processing framework (2013 edition) (Linux)

- To install and configure the Jetty server and JDK under Ubuntu 14.04.2 (Server)

- CentOS6.3 build a Python 3.3 environment to access Oracle 11gR2 (Database)

- JavaScript common array manipulation functions and usage (Programming)

- Configuring Haproxy log support (syslog logging support) (Server)

- Python decorators to learn and practice the actual usage scenarios (Programming)

- Some MySQL interview questions (Database)

- Linux firewall Iptables study notes (Linux)

- Installation of Gitlab under Ubuntu (Linux)

- CentOS 6.6 install rsync server (Server)

- 7 JavaScript interview questions (Programming)

- Everyone should know something about TCP (Linux)

- Puppet 3.5 Source package Installation and Configuration (Server)

- Category prevent DoS attacks against Linux (Linux)

- Differences Shell scripting languages and compiled languages (Programming)

- How to override the plain text files and directories soft connection in linux (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:      
 
- KVM add virtual disks (Linux)
- Let's Encrypt with semiautomatic into Nginx configuration https (Server)
- ARM platform compiler installation Golang (Linux)
- Depth understanding of JavaScript new mechanism (Programming)
- Linux iptables firewall settings whitelist (RHEL 6 and CentOS 7) (Linux)
- Slow update statement Performance Analysis (Database)
- The difference between IPython and Python (Linux)
- How to install Linux Go Language (Linux)
- Apache Web Security Linux systems (Linux)
- How LUKS encrypted disk / partition to perform remote incremental backup (Linux)
- How to manage the time and date at systemd Linux systems (Linux)
- Linux automatic installation and implementation (Linux)
- numpy and SciPy installation under Python for scientific computing package (Linux)
- Denyhosts prevent hackers using SSH scanning (Linux)
- How to use the tab in Vim carried Python code completion (Linux)
- Linux RPM (Linux)
- Android Scroller call mechanism and the relationship of computeScroll (Programming)
- Android source code compiled fatal error solutions (Programming)
- Ubuntu 14.04 can be used to create a WIFI hotspot for Android (Linux)
- Java implementation heap sort (large root heap) (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.