Difference between String Interpolation and Property Binding

String Interpolation

  • used for concatenating String
  • can do small math calculation like {{ 1+1 }} inside string interpolation
  • So expression inside {{ brackets }} are evaluated

Property binding

  • For Setting properties using [] brackets

String interpolation and Property Binding

String Interpolation is a special syntax which is converted to property binding by Angular. It’s a convenient alternative to property binding.

When you need to concatenate strings, you must use interpolation instead of property binding.

@Component({  
        selector: 'my-app',  
        template: `<div>  
                        <h1>{{interposlationExample}}</h1>  
                    </div>`  
    })  
    export class AppComponent {  
        interposlationExample: string = 'Interpolation for string only';  
      
    }  

Property Binding is used when you have to set an element property to a non-string data value.

Example:

In the following example, we disable a button by binding to the Boolean property isDisabled.

  import { Component } from '@angular/core';  
    @Component({  
        selector: 'my-app',  
        template: `<div>  
        <button [disabled]='isDisabled'>Disable me</button>  
                         </div>`  
    })  
    export class AppComponent {  
    isDisabled: boolean = true;  
    }

If you use interpolation instead of property binding, the button will always be disabled regardless isDisabled class property value is true or false.

import { Component } from '@angular/core';  
@Component({  
    selector: 'my-app',  
    template: `<div>  
    <button disabled='{{isDisabled}}'>Disable Me</button>  
                     </div>`  
})  
export class AppComponent {  
isDisabled: boolean = true/false;  
}

Reference :

Different Folders and Files in Angular

  1. e2e
    • All the files related to End to End Testing
  2. Node Modules
    • All the node libraries are stored in this folder
  3. src
    • The angular code which is going to be developed is written inside this
  4. .browserslistrc
    • List of browsers on which the application will run.
  5. .editorconfig
    • The Editer configuration on which this code will run
  6. .gitignore
    • Files to be ignored while commiting
  7. angular.json
    • All the Angular configuration is stored inside this file
  8. karma.conf.js
    • Test runner
  9. package-lock.json
    • All the node files downloaded are mentioned inside this file with version
  10. package.json
    • All the node files downloaded are mentioned but with loose versioning
  11. tsconfig.json
    • TypeScript configuration file
  12. tsconfig.app.json
    • TypeScript configuration file but specific to an app
    • Used when there are multiple applications inside a single angular application
  13. tslint.json
    • Lint configuration for ts file. (Maintaining code standard)

Files inside SRC folder

  1. app folder
    • We write all the angular application code inside this folder only
  2. assets folder
    • All the images and static json files are stored in this folder
  3. environments
    • Environment specific files
  4. favicon.ico
    • Icon displayed on the top of the browser
  5. index.html
    • This is the main html file in which components are loaded one by one
  6. main.ts
    • Angular application bootstrapping
  7. polyfill.ts
    • Contains the code to run Angular app in older or those browsers which does not yet support latest standards of web
  8. style.css
    • Global css file of Angular app
  9. test.ts
    • Configure testing framework using this

Zips & Tar

  • Creating zip
Zipping of fileszip backup.zip file1 file2 file3creates a zip containing the 3 files
Delete the original file after zipzip -m mybackup.zip file1Removes file1 after zipping it into backup.zip
Append a file in zip archivezip -u mybackup.zip file2Adds file2 to an existing zip mybackup.zip
Remove a file from a zip filezip -d mybackup.zip file1Removes file1 from mybackup.zip
Zipping a Directory recursivelyzip -r backupDir.zip dir1/ dir2/Zips the directories dir1 and dir2
  • Unzip commands
Basic unzipunzip mybackup.zip
Unzip content in a particular directoryunzip -d /home/directory/ mybackup.zipUnzips the content into /home/directory/ directory
List the contents of the zip fileunzip -l mybackup.ziplists the content of the zip file without extracting it
  • Gzip command

Compresses single file only. GZIP has a higher compression ration compared to normal zip.

Idea is to tar the required files and then gzip the single tar file.

GZIP removes the original file and creates a “.gz” file.

gzip myFile.txtcreates a myFile.txt.gz file and the original myFile.txt gets deleted.
gunzip myFile.txt.gzextracts the original file myFile.txt and myFile.txt.gz file gets removed.
  • Tar command

Stands for Tape Archive used for creating or extracting Archive files. Archive files are used to collect multiple data files together into single file for easier portability & storage.

Create a tar archivetar -cvf arch.tar *.javacreates a “arch.tar” archive file containing all the “.java” files present in the directory.
-c : Creates archive
-v : Verbose information
-f : given filename
Update existing tar filetar -rvf arch.tar *.htmladds all the “.html” files into existing tar “arch.tar”
Extract tar filetar -xvf arch.tarextracts the content of “arch.tar” into the current directory.
-x : Extract the archive
Extract tar file in specific directorytar -xvf arch.tar -C /home/directory/Extracts the content of the tar into directory “/home/directory/”
List tar content without extractiontar -tvf arch.tarLists the entire content of the “arch.tar” file

MAP mapping in JPA

A Map is an interface in which a unique key is associated with each value object. Thus, operations like search, update, delete are performed on the basis of key.

Map Mapping Example

In this example, we embed an object in an entity class and define it as a collection type Map.

  1. private Map<Integer,Address> map=new HashMap<Integer,Address>();  

This example contains the following steps: –

  • Create an entity class Employee.java under com.javatpoint.jpa package that contains employee id, name and embedded object (employee Address). The annotation @ElementCollection represents the embedded object.

Employee.java

  1. package com.javatpoint.jpa;  
  2. import java.util.*;  
  3.   
  4. import javax.persistence.*;  
  5. @Entity  
  6.   
  7. public class Employee {  
  8.   
  9.     @Id  
  10.     @GeneratedValue(strategy=GenerationType.AUTO)  
  11.     private int e_id;  
  12.     private String e_name;  
  13.       
  14.     @ElementCollection  
  15.     private Map<Integer,Address> map=new HashMap<Integer,Address>();  
  16.   
  17.       
  18.     public int getE_id() {  
  19.         return e_id;  
  20.     }  
  21.   
  22.     public void setE_id(int e_id) {  
  23.         this.e_id = e_id;  
  24.     }  
  25.   
  26.     public String getE_name() {  
  27.         return e_name;  
  28.     }  
  29.   
  30.     public void setE_name(String e_name) {  
  31.         this.e_name = e_name;  
  32.     }  
  33.   
  34.     public Map<Integer, Address> getMap() {  
  35.         return map;  
  36.     }  
  37.   
  38.     public void setMap(Map<Integer, Address> map) {  
  39.         this.map = map;  
  40.     }  
  41.   
  42.       
  43.       
  44. }  
  • Now, create a class of embedded object Address.java under com.javatpoint.jpa package. The annotation @Embeddable represents the embeddable object.

Address.java

  1. package com.javatpoint.jpa;  
  2. import javax.persistence.*;  
  3.   
  4. @Embeddable  
  5. public class Address {  
  6.   
  7.     private int e_pincode;  
  8.     private String e_city;  
  9.     private String e_state;  
  10.     public int getE_pincode() {  
  11.         return e_pincode;  
  12.     }  
  13.     public void setE_pincode(int e_pincode) {  
  14.         this.e_pincode = e_pincode;  
  15.     }  
  16.     public String getE_city() {  
  17.         return e_city;  
  18.     }  
  19.     public void setE_city(String e_city) {  
  20.         this.e_city = e_city;  
  21.     }  
  22.     public String getE_state() {  
  23.         return e_state;  
  24.     }  
  25.     public void setE_state(String e_state) {  
  26.         this.e_state = e_state;  
  27.     }  
  28.       
  29.       
  30.       
  31. }  
  • Now, map the entity class and other databases confiuguration in Persistence.xml file.

Persistence.xml

  1. <persistence>  
  2. <persistence-unit name=”Collection_Type”>  
  3.       
  4.      <class>com.javatpoint.jpa.Employee</class>  
  5.       <class>com.javatpoint.jpa.Address</class>  
  6.       
  7.   
  8.         
  9.      <properties>  
  10.       <property name=”javax.persistence.jdbc.driver” value=”com.mysql.jdbc.Driver”/>  
  11.          <property name=”javax.persistence.jdbc.url” value=”jdbc:mysql://localhost:3306/collection_mapping”/>  
  12.          <property name=”javax.persistence.jdbc.user” value=”root”/>  
  13.          <property name=”javax.persistence.jdbc.password” value=””/>  
  14.          <property name=”eclipselink.logging.level” value=”SEVERE”/>  
  15.          <property name=”eclipselink.ddl-generation” value=”create-or-extend-tables”/>  
  16.       </properties>  
  17.       
  18.     </persistence-unit>  
  19. </persistence>  
  • Create a persistence class MapMapping.java under com.javatpoint.collection package to persist the entity object with data.

MapMapping.java

  1. package com.javatpoint.collection;  
  2. import javax.persistence.*;  
  3.   
  4.   
  5. import com.javatpoint.jpa.*;  
  6. public class ListMapping{  
  7.   
  8.     public static void main(String[] args) {  
  9.           
  10.         EntityManagerFactory emf=Persistence.createEntityManagerFactory(“Collection_Type”);  
  11.         EntityManager em=emf.createEntityManager();  
  12.           
  13.         em.getTransaction().begin();  
  14.           
  15.           
  16.   
  17.           
  18.         Address a1=new Address();  
  19.         a1.setE_pincode(201301);  
  20.         a1.setE_city(“Noida”);  
  21.         a1.setE_state(“Uttar Pradesh”);  
  22.           
  23.           
  24.           
  25.           
  26.           
  27.         Address a2=new Address();  
  28.         a2.setE_pincode(302001);  
  29.         a2.setE_city(“Jaipur”);  
  30.         a2.setE_state(“Rajasthan”);  
  31.           
  32.         Address a3=new Address();  
  33.         a3.setE_pincode(133301);  
  34.         a3.setE_city(“Chandigarh”);  
  35.         a3.setE_state(“Punjab”);  
  36.       
  37.         Address a4=new Address();  
  38.         a4.setE_pincode(80001);  
  39.         a4.setE_city(“Patna”);  
  40.         a4.setE_state(“Bihar”);  
  41.           
  42.           
  43.     Employee e1=new Employee();  
  44.     e1.setE_id(1);  
  45.     e1.setE_name(“Vijay”);  
  46.       
  47.       
  48.     Employee e2=new Employee();  
  49.     e2.setE_id(2);  
  50.     e2.setE_name(“Vijay”);  
  51.       
  52.     Employee e3=new Employee();  
  53.     e3.setE_id(3);  
  54.     e3.setE_name(“William”);  
  55.       
  56.     Employee e4=new Employee();  
  57.     e4.setE_id(4);  
  58.     e4.setE_name(“Rahul”);  
  59.       
  60.     e1.getMap().put(1, a1);  
  61.     e2.getMap().put(2, a2);  
  62.     e3.getMap().put(3, a3);  
  63.     e4.getMap().put(4, a4);  
  64.       
  65.     em.persist(e1);  
  66.     em.persist(e2);  
  67.     em.persist(e3);  
  68.     em.persist(e4);  
  69.       
  70.     em.getTransaction().commit();  
  71.       
  72.     em.close();  
  73.     emf.close();  
  74.           
  75.           
  76.           
  77.     }  
  78.       
  79. }  

Output:

After the execution of the program, the following tables are generated under MySQL workbench.

  • Employee table – This table contains the employee details. To fetch data, run select * from employee query in MySQL.
JPA Map Mapping
  • Employee_map table – This table represents the mapping between employee and address table. The data in the table is arranged in an unordered manner.To fetch data, run select * from employee_map query in MySQL.
JPA Map Mapping

Set Mapping in JPA

A Set is an interface that contains unique elements. These elements don’t maintain any order. A Set can be used when there is a requirement of retrieving unique elements in an unordered manner.

Set Mapping Example

In this example, we embed an object in an entity class and define it as a collection type List.

  1. private Set<Address> address=new HashSet<Address>();  

This example contains the following steps: –

  • Create an entity class Employee.java under com.javatpoint.jpa package that contains employee id, name and embedded object (employee Address). The annotation @ElementCollection represents the embedded object.

Employee.java

  1. package com.javatpoint.jpa;  
  2. import java.util.*;  
  3.   
  4. import javax.persistence.*;  
  5. @Entity  
  6.   
  7. public class Employee {  
  8.   
  9.     @Id  
  10.     @GeneratedValue(strategy=GenerationType.AUTO)  
  11.     private int e_id;  
  12.     private String e_name;  
  13.       
  14.     @ElementCollection  
  15.     private Set<Address> address=new HashSet<Address>();  
  16.   
  17.       
  18.     public int getE_id() {  
  19.         return e_id;  
  20.     }  
  21.   
  22.     public void setE_id(int e_id) {  
  23.         this.e_id = e_id;  
  24.     }  
  25.   
  26.     public String getE_name() {  
  27.         return e_name;  
  28.     }  
  29.   
  30.     public void setE_name(String e_name) {  
  31.         this.e_name = e_name;  
  32.     }  
  33.   
  34.     public Set<Address> getAddress() {  
  35.         return address;  
  36.     }  
  37.   
  38.     public void setAddress(Set<Address> address) {  
  39.         this.address = address;  
  40.     }  
  41.   
  42.       
  43. }  
  • Now, create a class of embedded object Address.java under com.javatpoint.jpa package. The annotation @Embeddable represents the embeddable object.

Address.java

  1. package com.javatpoint.jpa;  
  2. import javax.persistence.*;  
  3.   
  4. @Embeddable  
  5. public class Address {  
  6.   
  7.     private int e_pincode;  
  8.     private String e_city;  
  9.     private String e_state;  
  10.     public int getE_pincode() {  
  11.         return e_pincode;  
  12.     }  
  13.     public void setE_pincode(int e_pincode) {  
  14.         this.e_pincode = e_pincode;  
  15.     }  
  16.     public String getE_city() {  
  17.         return e_city;  
  18.     }  
  19.     public void setE_city(String e_city) {  
  20.         this.e_city = e_city;  
  21.     }  
  22.     public String getE_state() {  
  23.         return e_state;  
  24.     }  
  25.     public void setE_state(String e_state) {  
  26.         this.e_state = e_state;  
  27.     }  
  28.       
  29.       
  30.       
  31. }  
  • Now, map the entity class and other databases confiuguration in Persistence.xml file.

Persistence.xml

  1. <persistence>  
  2. <persistence-unit name=”Collection_Type”>  
  3.       
  4.     <class>com.javatpoint.jpa.Employee</class>  
  5. <class>com.javatpoint.jpa.Address</class>  
  6.   
  7.   
  8.   
  9. <properties>  
  10. <property name=”javax.persistence.jdbc.driver” value=”com.mysql.jdbc.Driver”/>  
  11. <property name=”javax.persistence.jdbc.url” value=”jdbc:mysql://localhost:3306/collection_mapping”/>  
  12. <property name=”javax.persistence.jdbc.user” value=”root”/>  
  13. <property name=”javax.persistence.jdbc.password” value=””/>  
  14. <property name=”eclipselink.logging.level” value=”SEVERE”/>  
  15. <property name=”eclipselink.ddl-generation” value=”create-or-extend-tables”/>  
  16. </properties>  
  17.       
  18.     </persistence-unit>  
  19. </persistence>  
  • Create a persistence class ListMapping.java under com.javatpoint.collection package to persist the entity object with data.

SetMapping.java

  1. package com.javatpoint.collection;  
  2. import javax.persistence.*;  
  3.   
  4.   
  5. import com.javatpoint.jpa.*;  
  6. public class SetMapping{  
  7.   
  8.     public static void main(String[] args) {  
  9.           
  10.         EntityManagerFactory emf=Persistence.createEntityManagerFactory(“Collection_Type”);  
  11.         EntityManager em=emf.createEntityManager();  
  12.           
  13.         em.getTransaction().begin();  
  14.           
  15.           
  16.   
  17.           
  18.         Address a1=new Address();  
  19.         a1.setE_pincode(201301);  
  20.         a1.setE_city(“Noida”);  
  21.         a1.setE_state(“Uttar Pradesh”);  
  22.           
  23.           
  24.           
  25.           
  26.           
  27.         Address a2=new Address();  
  28.         a2.setE_pincode(302001);  
  29.         a2.setE_city(“Jaipur”);  
  30.         a2.setE_state(“Rajasthan”);  
  31.           
  32.         Address a3=new Address();  
  33.         a3.setE_pincode(133301);  
  34.         a3.setE_city(“Chandigarh”);  
  35.         a3.setE_state(“Punjab”);  
  36.       
  37.         Address a4=new Address();  
  38.         a4.setE_pincode(80001);  
  39.         a4.setE_city(“Patna”);  
  40.         a4.setE_state(“Bihar”);  
  41.           
  42.           
  43.     Employee e1=new Employee();  
  44.     e1.setE_id(1);  
  45.     e1.setE_name(“Vijay”);  
  46.       
  47.       
  48.     Employee e2=new Employee();  
  49.     e2.setE_id(2);  
  50.     e2.setE_name(“Vijay”);  
  51.       
  52.     Employee e3=new Employee();  
  53.     e3.setE_id(3);  
  54.     e3.setE_name(“William”);  
  55.       
  56.     Employee e4=new Employee();  
  57.     e4.setE_id(4);  
  58.     e4.setE_name(“Rahul”);  
  59.       
  60.     e1.getAddress().add(a1);  
  61.     e2.getAddress().add(a2);  
  62.     e3.getAddress().add(a3);  
  63.     e4.getAddress().add(a4);  
  64.       
  65.     em.persist(e1);  
  66.     em.persist(e2);  
  67.     em.persist(e3);  
  68.     em.persist(e4);  
  69.       
  70.     em.getTransaction().commit();  
  71.       
  72.     em.close();  
  73.     emf.close();  
  74.           
  75.           
  76.           
  77.     }  
  78.       
  79. }  

Output:

After the execution of the program, the following tables are generated under MySQL workbench.

  • Employee table – This table contains the employee details. To fetch data, run select * from employee query in MySQL.
JPA Set Mapping
  • Employee_address table – This table represents the mapping between employee and address table. The data in the table is arranged in an unordered manner.To fetch data, run select * from employee_address query in MySQL.
JPA Set Mapping

List Mapping in JPA

A List is an interface which is used to insert and delete elements on the basis of index. It can be used when there is a requirement of retrieving elements in a user-defined order.

List Mapping Example

In this example, we embed an object in an entity class and define it as a collection type List.

  1. private List<Address> address=new ArrayList<Address>();  

This example contains the following steps: –

  • Create an entity class Employee.java under com.javatpoint.jpa package that contains employee id, name and embedded object (employee Address). The annotation @ElementCollection represents the embedded object.

Employee.java

  1. package com.javatpoint.jpa;  
  2. import java.util.*;  
  3.   
  4. import javax.persistence.*;  
  5. @Entity  
  6.   
  7. public class Employee {  
  8.   
  9.     @Id  
  10.     @GeneratedValue(strategy=GenerationType.AUTO)  
  11.     private int e_id;  
  12.     private String e_name;  
  13.       
  14.     @ElementCollection  
  15.     private List<Address> address=new ArrayList<Address>();  
  16.   
  17.       
  18.     public int getE_id() {  
  19.         return e_id;  
  20.     }  
  21.   
  22.     public void setE_id(int e_id) {  
  23.         this.e_id = e_id;  
  24.     }  
  25.   
  26.     public String getE_name() {  
  27.         return e_name;  
  28.     }  
  29.   
  30.     public void setE_name(String e_name) {  
  31.         this.e_name = e_name;  
  32.     }  
  33.   
  34.     public List<Address> getAddress() {  
  35.         return address;  
  36.     }  
  37.   
  38.     public void setAddress(List<Address> address) {  
  39.         this.address = address;  
  40.     }  
  41.   
  42.       
  43. }  
  • Now, create a class of embedded object Address.java under com.javatpoint.jpa package. The annotation @Embeddable represents the embeddable object.

Address.java

  1. package com.javatpoint.jpa;  
  2. import javax.persistence.*;  
  3.   
  4. @Embeddable  
  5. public class Address {  
  6.   
  7.     private int e_pincode;  
  8.     private String e_city;  
  9.     private String e_state;  
  10.     public int getE_pincode() {  
  11.         return e_pincode;  
  12.     }  
  13.     public void setE_pincode(int e_pincode) {  
  14.         this.e_pincode = e_pincode;  
  15.     }  
  16.     public String getE_city() {  
  17.         return e_city;  
  18.     }  
  19.     public void setE_city(String e_city) {  
  20.         this.e_city = e_city;  
  21.     }  
  22.     public String getE_state() {  
  23.         return e_state;  
  24.     }  
  25.     public void setE_state(String e_state) {  
  26.         this.e_state = e_state;  
  27.     }  
  28.       
  29.       
  30.       
  31. }  
  • Now, map the entity class and other databases confiuguration in Persistence.xml file.

Persistence.xml

  1. <persistence>  
  2. <persistence-unit name=”Collection_Type”>  
  3.       
  4.     <class>com.javatpoint.jpa.Employee</class>  
  5. <class>com.javatpoint.jpa.Address</class>  
  6.   
  7.   
  8.   
  9. <properties>  
  10. <property name=”javax.persistence.jdbc.driver” value=”com.mysql.jdbc.Driver”/>  
  11. <property name=”javax.persistence.jdbc.url” value=”jdbc:mysql://localhost:3306/collection_mapping”/>  
  12. <property name=”javax.persistence.jdbc.user” value=”root”/>  
  13. <property name=”javax.persistence.jdbc.password” value=””/>  
  14. <property name=”eclipselink.logging.level” value=”SEVERE”/>  
  15. <property name=”eclipselink.ddl-generation” value=”create-or-extend-tables”/>  
  16. </properties>  
  17.       
  18.     </persistence-unit>  
  19. </persistence>  
  • Create a persistence class ListMapping.java under com.javatpoint.collection package to persist the entity object with data.

ListMapping.java

  1. package com.javatpoint.collection;  
  2. import javax.persistence.*;  
  3.   
  4.   
  5. import com.javatpoint.jpa.*;  
  6. public class ListMapping{  
  7.   
  8.     public static void main(String[] args) {  
  9.           
  10.         EntityManagerFactory emf=Persistence.createEntityManagerFactory(“Collection_Type”);  
  11.         EntityManager em=emf.createEntityManager();  
  12.           
  13.         em.getTransaction().begin();  
  14.           
  15.           
  16.   
  17.           
  18.         Address a1=new Address();  
  19.         a1.setE_pincode(201301);  
  20.         a1.setE_city(“Noida”);  
  21.         a1.setE_state(“Uttar Pradesh”);  
  22.           
  23.           
  24.           
  25.           
  26.           
  27.         Address a2=new Address();  
  28.         a2.setE_pincode(302001);  
  29.         a2.setE_city(“Jaipur”);  
  30.         a2.setE_state(“Rajasthan”);  
  31.           
  32.       
  33.           
  34.     Employee e1=new Employee();  
  35.     e1.setE_id(1);  
  36.     e1.setE_name(“Vijay”);  
  37.     e1.getAddress().add(a1);  
  38.       
  39.     Employee e2=new Employee();  
  40.     e2.setE_id(2);  
  41.     e2.setE_name(“John”);  
  42.     e2.getAddress().add(a2);  
  43.       
  44.     em.persist(e1);  
  45.     em.persist(e2);  
  46.       
  47.     em.getTransaction().commit();  
  48.       
  49.     em.close();  
  50.     emf.close();  
  51.           
  52.           
  53.           
  54.     }  
  55.       
  56. }  

Output:

After the execution of the program, the following table are generated under MySQL workbench.

  • Employee table – This table contains the employee details. To fetch data, run select * from employee query in MySQL.
JPA List Mapping
  • Employee_address table – This table represents the mapping between employee and address table. To fetch data, run select * from employee_address query in MySQL.
JPA List Mapping