广州北大青鸟计算机职业培训学校
互联网技术培训、软件技术培训、大数据培训、云计算培训、数据分析培训信息网
当前位置:网站首页 > 软件教程 > Java技术 > 正文

惠州Java中的JPA嵌入式共享实例_北大青鸟IT学校

作者:邓华发布时间:2021-05-06分类:Java技术浏览:804


导读:Java是现在非常主流的编程语言之一,很多人想转行学习Java。那么下面惠州北大青鸟老师给大家分Java中的JPA嵌入式共享实例,希望对大家的学习有帮助。

Java是现在非常主流的编程语言之一,很多人想转行学习Java。那么下面惠州北大青鸟老师给大家分Java中的JPA嵌入式共享实例,希望对大家的学习有帮助。

实例

以下代码来自Address.java。

package cn.w3cschool.common;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Embeddable;

@Embeddable @Access(AccessType.FIELD)public class Address {    private String street;
   private String city;
   private String state;
   @Column(name="ZIP_CODE")    private String zip;    public String getStreet() {
       return street;
   }    
   public void setStreet(String address) {
       this.street = 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 String getZip() {
       return zip;
   }    public void setZip(String zip) {
       this.zip = zip;
   }    public String toString() {
       return "Address street: " + getStreet() +               ", city: " + getCity() +               ", state: " + getState() +               ", zip: " + getZip();
   }

}

以下代码来自Employee.java。

package cn.w3cschool.common;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entitypublic class Employee {
 @Id private int id;  private String name;  private long salary;
 
 @Embedded
 @AttributeOverrides({
     @AttributeOverride(name = "state", column = @Column(name = "PROVINCE")),
     @AttributeOverride(name = "zip", column = @Column(name = "POSTAL_CODE"))
 })  private Address address;  
 public Employee() {}  public int getId() {
     return id;
 }  
 public void setId(int id) {
     this.id = id;
 }  
 public String getName() {
     return name;
 }  
 public void setName(String name) {
     this.name = name;
 }  public long getSalary() {
     return salary;
 }  public void setSalary(long salary) {
     this.salary = salary;
 }  public Address getAddress() {
     return address;
 }  
 public void setAddress(Address address) {
     this.address = address;
 }  public String toString() {
     return "Employee id: " + getId() + " name: " + getName() +             " salary: " + getSalary() + " address: " + getAddress();
 }
}

以下代码来自Company.java。

package cn.w3cschool.common;


import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.Id;


@Entitypublic class Company {
   @Id private int id;
   
   @Embedded    private Address address;    
   public int getId() {
       return id;
   }    public void setId(int id) {
       this.id = id;
   }    public Address getAddress() {
       return this.address;
   }    public void setAddress(Address address) {
       this.address = address;
   }    public String toString() {
       return "Company id: " + getId() + " address: " + getAddress();

   }
   
}

下面的代码来自PersonDaoImpl.java。

package cn.w3cschool.common;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.transaction.annotation.Transactional;

@Transactionalpublic class PersonDaoImpl {  public void test() {

   Employee emp = new Employee();
   emp.setName("name");
   emp.setSalary(12345);
   

   Address p = new Address();
   p.setCity("New York");

   emp.setAddress(p);
   
   Company c = new Company();
   c.setAddress(p);
   c.setId(1);
   
   em.persist(emp);
   
   em.persist(c);
 }

 @PersistenceContext  private EntityManager em;
}

以下是数据库转储。

Table Name: COMPANY
Row:
   Column Name: ID,
   Column Type: INTEGER:
   Column Value: 1

   Column Name: CITY,
   Column Type: VARCHAR:
   Column Value: New York

   Column Name: STATE,
   Column Type: VARCHAR:
   Column Value: null

   Column Name: STREET,
   Column Type: VARCHAR:
   Column Value: null

   Column Name: ZIP_CODE,
   Column Type: VARCHAR:
   Column Value: null





Table Name: EMPLOYEE
Row:
   Column Name: ID,
   Column Type: INTEGER:
   Column Value: 0

   Column Name: CITY,
   Column Type: VARCHAR:
   Column Value: New York

   Column Name: PROVINCE,
   Column Type: VARCHAR:
   Column Value: null

   Column Name: STREET,
   Column Type: VARCHAR:
   Column Value: null

   Column Name: POSTAL_CODE,
   Column Type: VARCHAR:
   Column Value: null

   Column Name: NAME,
   Column Type: VARCHAR:
   Column Value: name

   Column Name: SALARY,
   Column Type: BIGINT:
   Column Value: 12345

想了解更多关于Java的资讯吗?可以来惠州北大青鸟新方舟校区了解一下。

Java13.png

Java

标签:惠州计算机JAVA软件开发惠州计算机Java软件开发惠州计算机JAVA培训惠州计算机JAVA软件开发学校惠州计算机Java软件开发培训JAVAJava软件开发北大青鸟IT计算机学校北大青鸟IT软件学校北大青鸟IT学校


Java技术排行
标签列表
网站分类
文章归档
最近发表