Hibernate注解:一对一主键关联

情形:两个表,my_site和my_site_company,通过主键site_id唯一关联。my_site的主键是自动增加,my_site_company的主键依赖于my_site。

#
# Source for table my_site
#

DROP TABLE IF EXISTS `my_site`;
CREATE TABLE `my_site` (
  `site_id` int(11) NOT NULL AUTO_INCREMENT,
  `site_name` varchar(100) DEFAULT NULL COMMENT 站点名称,
  `short_name` varchar(100) DEFAULT NULL COMMENT 简短名称,
  PRIMARY KEY (`site_id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

#
# Source for table my_site_company
#

DROP TABLE IF EXISTS `my_site_company`;
CREATE TABLE `my_site_company` (
  `site_id` int(11) NOT NULL DEFAULT ‘0‘,
  `name` varchar(255) NOT NULL COMMENT 公司名称,
  `scale` varchar(255) DEFAULT NULL COMMENT 公司规模,
  `industry` varchar(1000) DEFAULT NULL COMMENT 公司行业,
  `contact` varchar(500) DEFAULT NULL COMMENT 联系方式,
  `address` varchar(500) DEFAULT NULL COMMENT 公司地址,
  PRIMARY KEY (`site_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

Site.java:

@Entity
@Table(name = "my_site")
@SuppressWarnings("serial")
public class Site implements Serializable
{ 

    // Site的主键是自动递增
    @Id
    @GeneratedValue
    @Column(name = "site_id")
    private Integer siteId;
    
    @Column(name = "site_name")
    private String siteName;
    
    @Column(name = "short_name")
    private String shortName;
    
    // Site通过主键一对一关联到Company, 
    // 在主键自动递增的对象这边写
    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    private Company company; 
}

Company.java:

@Entity
@Table(name = "my_site_company")
@SuppressWarnings("serial")
public class Company implements Serializable
{ 
    // Company的主键来源于site的主键
    @Id
    @Column(name = "site_id")
    @GenericGenerator(name = "pkGenerator",
        strategy = "foreign" ,
        parameters = {@Parameter(name = "property", value = "site")})  
    @GeneratedValue(generator = "pkGenerator")
    private Integer siteId;
    
    @Column
    private String name;
    
    @Column
    private String scale;
    
    @Column
    private String industry;
    
    @Column
    private String address;
    
    @Column
    private String contact;

    @OneToOne(cascade = CascadeType.ALL, 
             mappedBy = "company")  
    private Site site;
}

CompanyController.java:

public class CompanyController extends SimpleController
{
    @Autowired
    SiteDao siteDao;
    
    @Autowired
    CompanyDao companyDao;
    
    @RequestMapping
    public String indexAction(Model model,
            HttpServletRequest request) throws Exception
    {
        Site site = siteDao.fetchOne(1);
        Company company = site.getCompany();
        if (company == null)
            company = new Company();
        
        if (request.getMethod() != "POST")
        {
            model.addAttribute("entity", company);
            return redirectDefaultView();
        }
        
        RequestUtils.copyBeanProperties(company);
        company.setSite(site);
        site.setCompany(company);
        companyDao.update(company);
        
        return redirectIndex(SUCCESS_SAVE);
    }
    
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。