Hibernate @OneToOne Not Mapping

时间:2015-06-24 21:30:44

标签: java sql-server spring hibernate orm

错误

  

org.hibernate.AnnotationException:未知的mappedBy in:com.catalyst.User.Model.Person.User,引用的属性未知:   com.catalyst.User.Model.User.User_ID

我正在尝试创建OneToOne关系但没有成功。我做错了什么?

User.java

package com.catalyst.User.Model;

import javax.persistence.CascadeType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;

@Entity(name = "User" )
@Table(name = "dbo.Users")
public class User
{
    @Id
    @Column(name="ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int ID;

    @Column(name="Username")
    private String Username;

    @Column(name="Password")
    private String Password;

    @Column(name="Active")
    private int Active;

    @OneToOne(cascade = CascadeType.ALL)
    @PrimaryKeyJoinColumn
    private Person hPerson;

    public User()
    {
        // Hibernate Needs This
    }

    public User(String argUsername, String argPassword, int argActive)
    {
        this.Username   = argUsername;
        this.Password   = argPassword;
        this.Active     = argActive;
    }

    public Person getPerson()
    {
        return(hPerson);
    }

    public String getUsername()
    {
        return(Username);
    }

    public String getPassword()
    {
        return(Password);
    }

    public int getActive()
    {
        return(Active);
    }

    public void setUsername(String argUsername)
    {
        this.Username   = argUsername;
    }

    public void setPassword(String argPassword)
    {
        this.Password   = argPassword;
    }

    public void setActive(int argActive)
    {
        this.Active     = argActive;
    }

}

Person.java

package com.catalyst.User.Model;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

/*
    Person Model
*/

@Entity(name = "Person" )
@Table(name = "dbo.Person")
public class Person
{
    @Id
    @GeneratedValue(generator = "foreigngen")
    @GenericGenerator(strategy = "foreign", name="foreigngen",parameters = @Parameter(name = "property", value="user"))
    @Column(name = "ID")
    private int ID;

    @Column(name="FName")
    private String Firstname;

    @Column(name="LName")
    private String Lastname;

    @Column(name="Address")
    private String Address;

    @Column(name="Email")
    private String Email;

    @OneToOne(mappedBy = "user")
    private User User;

    @Column(name="Role_Id")
    private int RoleID;

    public Person()
    {
        // Hibernate Needs This
    }

    public Person(String argFName, String argLName, String argAddress, String argEmail, int argRoleID)
    {
        this.Firstname      = argFName;
        this.Lastname       = argLName;
        this.Address        = argAddress;
        this.Email          = argEmail;
        this.RoleID         = argRoleID;
    }

    public int getID()
    {
        return(ID);
    }

    public String getFirstname()
    {
        return(Firstname);
    }

    public String getLastname()
    {
        return(Lastname);
    }

    public String getFullname()
    {
        return(Firstname + " " + Lastname);
    }

    public String getAddress()
    {
        return(Address);
    }

    public String getEmail()
    {
        return(Email);
    }

    public User getUser()
    {
        return(User);
    }

    public int getRoleID()
    {
        return(RoleID);
    }

    public void setID(int arg)
    {
        this.ID = arg;
    }

    public void setFirstname(String arg)
    {
        this.Firstname = arg;
    }

    public void setLastname(String arg)
    {
        this.Lastname = arg;
    }

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

    public void setEmail(String arg)
    {
        this.Email = arg;
    }

    public void setUserID(User arg)
    {
        this.User = arg;
    }

    public void setRoleID(int arg)
    {
        this.RoleID = arg;
    }
}

enter image description here

数据库转储

USE [master]
GO
/****** Object:  Database [APP]    Script Date: 6/23/2015 1:56:37 PM ******/
CREATE DATABASE [APP]
 CONTAINMENT = NONE
 ON  PRIMARY 
( NAME = N'APP', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\APP.mdf' , SIZE = 4096KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG ON 
( NAME = N'APP_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\APP_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
ALTER DATABASE [APP] SET COMPATIBILITY_LEVEL = 110
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [APP].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [APP] SET ANSI_NULL_DEFAULT OFF 
GO
ALTER DATABASE [APP] SET ANSI_NULLS OFF 
GO
ALTER DATABASE [APP] SET ANSI_PADDING OFF 
GO
ALTER DATABASE [APP] SET ANSI_WARNINGS OFF 
GO
ALTER DATABASE [APP] SET ARITHABORT OFF 
GO
ALTER DATABASE [APP] SET AUTO_CLOSE OFF 
GO
ALTER DATABASE [APP] SET AUTO_CREATE_STATISTICS ON 
GO
ALTER DATABASE [APP] SET AUTO_SHRINK OFF 
GO
ALTER DATABASE [APP] SET AUTO_UPDATE_STATISTICS ON 
GO
ALTER DATABASE [APP] SET CURSOR_CLOSE_ON_COMMIT OFF 
GO
ALTER DATABASE [APP] SET CURSOR_DEFAULT  GLOBAL 
GO
ALTER DATABASE [APP] SET CONCAT_NULL_YIELDS_NULL OFF 
GO
ALTER DATABASE [APP] SET NUMERIC_ROUNDABORT OFF 
GO
ALTER DATABASE [APP] SET QUOTED_IDENTIFIER OFF 
GO
ALTER DATABASE [APP] SET RECURSIVE_TRIGGERS OFF 
GO
ALTER DATABASE [APP] SET  DISABLE_BROKER 
GO
ALTER DATABASE [APP] SET AUTO_UPDATE_STATISTICS_ASYNC OFF 
GO
ALTER DATABASE [APP] SET DATE_CORRELATION_OPTIMIZATION OFF 
GO
ALTER DATABASE [APP] SET TRUSTWORTHY OFF 
GO
ALTER DATABASE [APP] SET ALLOW_SNAPSHOT_ISOLATION OFF 
GO
ALTER DATABASE [APP] SET PARAMETERIZATION SIMPLE 
GO
ALTER DATABASE [APP] SET READ_COMMITTED_SNAPSHOT OFF 
GO
ALTER DATABASE [APP] SET HONOR_BROKER_PRIORITY OFF 
GO
ALTER DATABASE [APP] SET RECOVERY FULL 
GO
ALTER DATABASE [APP] SET  MULTI_USER 
GO
ALTER DATABASE [APP] SET PAGE_VERIFY CHECKSUM  
GO
ALTER DATABASE [APP] SET DB_CHAINING OFF 
GO
ALTER DATABASE [APP] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF ) 
GO
ALTER DATABASE [APP] SET TARGET_RECOVERY_TIME = 0 SECONDS 
GO
USE [APP]
GO
/****** Object:  Table [dbo].[AssociatedProcedure]    Script Date: 6/23/2015 1:56:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[AssociatedProcedure](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Type] [varchar](50) NOT NULL,
    [Devices_ID] [int] NULL,
    [MicSupply_ID] [int] NOT NULL,
    [Drugs_ID] [int] NOT NULL,
    [Active] [bit] NOT NULL,
 CONSTRAINT [PK_AssociatedProcedure] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Devices]    Script Date: 6/23/2015 1:56:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Devices](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [DevicesType] [varchar](50) NOT NULL,
    [Cost] [decimal](18, 0) NOT NULL,
    [DeviceAge] [int] NOT NULL,
    [Active] [bit] NOT NULL,
    [Max Age] [int] NOT NULL,
 CONSTRAINT [PK_Devices] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Drugs]    Script Date: 6/23/2015 1:56:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Drugs](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [DrugType] [varchar](50) NOT NULL,
    [DrugCost] [decimal](18, 0) NOT NULL,
    [Amount] [int] NOT NULL,
    [Active] [bit] NOT NULL,
 CONSTRAINT [PK_Drugs] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[HIstory]    Script Date: 6/23/2015 1:56:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[HIstory](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Schedule_ID] [int] NULL,
    [Active] [bit] NULL,
    [Pet_ID] [int] NULL,
    [Actice] [bit] NULL,
 CONSTRAINT [PK_HIstory] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[MicSupply]    Script Date: 6/23/2015 1:56:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[MicSupply](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [SupplyType] [varchar](50) NOT NULL,
    [Count] [int] NOT NULL,
    [Active] [bit] NOT NULL,
 CONSTRAINT [PK_MicSupply] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Person]    Script Date: 6/23/2015 1:56:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Person](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [FName] [varchar](50) NOT NULL,
    [LName] [varchar](50) NOT NULL,
    [Address] [varchar](50) NOT NULL,
    [Email] [varchar](50) NOT NULL,
    [User_ID] [int] NOT NULL,
    [Role_Id] [int] NOT NULL,
    [Active] [bit] NOT NULL,
 CONSTRAINT [PK_Person_1] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Pet]    Script Date: 6/23/2015 1:56:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Pet](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Prerson_ID] [int] NULL,
    [Name] [varchar](50) NULL,
    [Weight] [nchar](10) NULL,
    [Age] [bigint] NULL,
    [PetType] [varchar](50) NULL,
    [Active] [bit] NULL,
 CONSTRAINT [PK_Pet] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Procedure]    Script Date: 6/23/2015 1:56:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Procedure](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [AssociatedProcedure_ID] [int] NULL,
    [Cost] [decimal](18, 0) NOT NULL,
    [Devices_ID] [int] NOT NULL,
    [Drugs_ID] [int] NOT NULL,
    [MicSupply_ID] [int] NOT NULL,
    [Active] [bit] NOT NULL,
 CONSTRAINT [PK_Procedure] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[Role]    Script Date: 6/23/2015 1:56:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Role](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Role] [varchar](50) NOT NULL,
    [Active] [bit] NULL,
 CONSTRAINT [PK_Role] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Schedule]    Script Date: 6/23/2015 1:56:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Schedule](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Date] [datetime] NULL,
    [Procedure_ID] [int] NULL,
    [Pet_ID] [int] NULL,
    [Vet_ID] [int] NULL,
    [Devices_ID] [int] NULL,
    [Cost] [decimal](18, 0) NULL,
    [Services_ID] [int] NULL,
    [Active] [bit] NULL,
    [History_ID] [int] NULL,
 CONSTRAINT [PK_Schedule] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[Services]    Script Date: 6/23/2015 1:56:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Services](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Type] [varchar](50) NOT NULL,
    [Cost] [decimal](18, 0) NOT NULL,
    [Active] [bit] NOT NULL,
 CONSTRAINT [PK_Services] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[User]    Script Date: 6/23/2015 1:56:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[User](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Username] [varchar](50) NOT NULL,
    [Password] [varchar](50) NOT NULL,
    [Active] [bit] NOT NULL,
 CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[Vets]    Script Date: 6/23/2015 1:56:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Vets](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Role_ID] [int] NOT NULL,
    [Active] [bit] NOT NULL,
 CONSTRAINT [PK_Vets] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET IDENTITY_INSERT [dbo].[AssociatedProcedure] ON 

INSERT [dbo].[AssociatedProcedure] ([ID], [Type], [Devices_ID], [MicSupply_ID], [Drugs_ID], [Active]) VALUES (14, N'Wrap', 2, 3, 2, 1)
INSERT [dbo].[AssociatedProcedure] ([ID], [Type], [Devices_ID], [MicSupply_ID], [Drugs_ID], [Active]) VALUES (15, N'stich', NULL, 3, 4, 1)
SET IDENTITY_INSERT [dbo].[AssociatedProcedure] OFF
SET IDENTITY_INSERT [dbo].[Devices] ON 

INSERT [dbo].[Devices] ([ID], [DevicesType], [Cost], [DeviceAge], [Active], [Max Age]) VALUES (1, N'Xray', CAST(100 AS Decimal(18, 0)), 3, 1, 101)
INSERT [dbo].[Devices] ([ID], [DevicesType], [Cost], [DeviceAge], [Active], [Max Age]) VALUES (2, N'Table', CAST(50 AS Decimal(18, 0)), 1, 1, 122)
INSERT [dbo].[Devices] ([ID], [DevicesType], [Cost], [DeviceAge], [Active], [Max Age]) VALUES (3, N'Light', CAST(25 AS Decimal(18, 0)), 5, 1, 133)
SET IDENTITY_INSERT [dbo].[Devices] OFF
SET IDENTITY_INSERT [dbo].[Drugs] ON 

INSERT [dbo].[Drugs] ([ID], [DrugType], [DrugCost], [Amount], [Active]) VALUES (1, N'Epoch', CAST(4 AS Decimal(18, 0)), 200, 1)
INSERT [dbo].[Drugs] ([ID], [DrugType], [DrugCost], [Amount], [Active]) VALUES (2, N'dg health hemorrhoidal', CAST(3 AS Decimal(18, 0)), 300, 1)
INSERT [dbo].[Drugs] ([ID], [DrugType], [DrugCost], [Amount], [Active]) VALUES (3, N'Sertraline Hydrochloride', CAST(4 AS Decimal(18, 0)), 100, 1)
INSERT [dbo].[Drugs] ([ID], [DrugType], [DrugCost], [Amount], [Active]) VALUES (4, N'ibuprofen', CAST(2 AS Decimal(18, 0)), 200, 1)
INSERT [dbo].[Drugs] ([ID], [DrugType], [DrugCost], [Amount], [Active]) VALUES (5, N'Hydromorphone HCl', CAST(5 AS Decimal(18, 0)), 500, 1)
SET IDENTITY_INSERT [dbo].[Drugs] OFF
SET IDENTITY_INSERT [dbo].[HIstory] ON 

INSERT [dbo].[HIstory] ([ID], [Schedule_ID], [Active], [Pet_ID], [Actice]) VALUES (4, 4, 1, 4, 1)
INSERT [dbo].[HIstory] ([ID], [Schedule_ID], [Active], [Pet_ID], [Actice]) VALUES (6, 5, 1, 3, 1)
SET IDENTITY_INSERT [dbo].[HIstory] OFF
SET IDENTITY_INSERT [dbo].[MicSupply] ON 

INSERT [dbo].[MicSupply] ([ID], [SupplyType], [Count], [Active]) VALUES (1, N'tape', 6, 1)
INSERT [dbo].[MicSupply] ([ID], [SupplyType], [Count], [Active]) VALUES (2, N'glue', 7, 1)
INSERT [dbo].[MicSupply] ([ID], [SupplyType], [Count], [Active]) VALUES (3, N'glove', 3, 1)
INSERT [dbo].[MicSupply] ([ID], [SupplyType], [Count], [Active]) VALUES (4, N'broom', 2, 1)
INSERT [dbo].[MicSupply] ([ID], [SupplyType], [Count], [Active]) VALUES (5, N'bleach', 8, 1)
SET IDENTITY_INSERT [dbo].[MicSupply] OFF
SET IDENTITY_INSERT [dbo].[Person] ON 

INSERT [dbo].[Person] ([ID], [FName], [LName], [Address], [Email], [User_ID], [Role_Id], [Active]) VALUES (4, N'Timothy', N'Stewart', N'476 Kedzie Drive', N'tstewart0@soup.io', 1, 3, 1)
INSERT [dbo].[Person] ([ID], [FName], [LName], [Address], [Email], [User_ID], [Role_Id], [Active]) VALUES (6, N'David', N'Hath', N'92990 Sutherland Crossing', N'hbryant9@europa.eu', 2, 1, 1)
INSERT [dbo].[Person] ([ID], [FName], [LName], [Address], [Email], [User_ID], [Role_Id], [Active]) VALUES (7, N'Nick', N'Tacey', N'242 Oakridge Street', N'achavez1@g.co', 3, 1, 1)
INSERT [dbo].[Person] ([ID], [FName], [LName], [Address], [Email], [User_ID], [Role_Id], [Active]) VALUES (8, N'Nick', N'Duncan', N'853 Mariners Cove Trail', N'ariley8@forbes.com', 4, 1, 1)
INSERT [dbo].[Person] ([ID], [FName], [LName], [Address], [Email], [User_ID], [Role_Id], [Active]) VALUES (10, N'James', N'Blair', N'1234', N'happy', 5, 1, 1)
INSERT [dbo].[Person] ([ID], [FName], [LName], [Address], [Email], [User_ID], [Role_Id], [Active]) VALUES (12, N'Anne', N'Chavez', N'242 Oakridge Street', N'achavez1@g.co', 6, 2, 1)
INSERT [dbo].[Person] ([ID], [FName], [LName], [Address], [Email], [User_ID], [Role_Id], [Active]) VALUES (16, N'Christine', N'Carroll', N'4 Hintze Center', N'ccarroll2@wikipedia.org', 7, 5, 1)
INSERT [dbo].[Person] ([ID], [FName], [LName], [Address], [Email], [User_ID], [Role_Id], [Active]) VALUES (17, N'Jonathan', N'George', N'07067 Pawling Lane', N'jgeorge3@fda.gov', 8, 4, 1)
SET IDENTITY_INSERT [dbo].[Person] OFF
SET IDENTITY_INSERT [dbo].[Pet] ON 

INSERT [dbo].[Pet] ([ID], [Prerson_ID], [Name], [Weight], [Age], [PetType], [Active]) VALUES (2, 6, N'Peter', N'3         ', 4, N'dog', 1)
INSERT [dbo].[Pet] ([ID], [Prerson_ID], [Name], [Weight], [Age], [PetType], [Active]) VALUES (3, 9, N'Phyllis', N'2         ', 7, N'cat', 1)
INSERT [dbo].[Pet] ([ID], [Prerson_ID], [Name], [Weight], [Age], [PetType], [Active]) VALUES (4, 9, N'Heather', N'4         ', 21, N'dog', 1)
INSERT [dbo].[Pet] ([ID], [Prerson_ID], [Name], [Weight], [Age], [PetType], [Active]) VALUES (5, 10, N'Patricia', N'14        ', 1, N'cat', 1)
SET IDENTITY_INSERT [dbo].[Pet] OFF
SET IDENTITY_INSERT [dbo].[Procedure] ON 

INSERT [dbo].[Procedure] ([ID], [AssociatedProcedure_ID], [Cost], [Devices_ID], [Drugs_ID], [MicSupply_ID], [Active]) VALUES (1, NULL, CAST(400 AS Decimal(18, 0)), 2, 2, 1, 1)
INSERT [dbo].[Procedure] ([ID], [AssociatedProcedure_ID], [Cost], [Devices_ID], [Drugs_ID], [MicSupply_ID], [Active]) VALUES (2, 14, CAST(600 AS Decimal(18, 0)), 3, 4, 1, 1)
INSERT [dbo].[Procedure] ([ID], [AssociatedProcedure_ID], [Cost], [Devices_ID], [Drugs_ID], [MicSupply_ID], [Active]) VALUES (4, NULL, CAST(300 AS Decimal(18, 0)), 1, 5, 2, 1)
SET IDENTITY_INSERT [dbo].[Procedure] OFF
SET IDENTITY_INSERT [dbo].[Role] ON 

INSERT [dbo].[Role] ([ID], [Role], [Active]) VALUES (1, N'Admin', 1)
INSERT [dbo].[Role] ([ID], [Role], [Active]) VALUES (2, N'Vet', 1)
INSERT [dbo].[Role] ([ID], [Role], [Active]) VALUES (3, N'Vet Tec', 1)
INSERT [dbo].[Role] ([ID], [Role], [Active]) VALUES (4, N'Divice Tec', 1)
INSERT [dbo].[Role] ([ID], [Role], [Active]) VALUES (5, N'Employe', 1)
SET IDENTITY_INSERT [dbo].[Role] OFF
SET IDENTITY_INSERT [dbo].[Schedule] ON 

INSERT [dbo].[Schedule] ([ID], [Date], [Procedure_ID], [Pet_ID], [Vet_ID], [Devices_ID], [Cost], [Services_ID], [Active], [History_ID]) VALUES (4, NULL, 1, 2, 1, 2, CAST(300 AS Decimal(18, 0)), 1, 1, 1)
INSERT [dbo].[Schedule] ([ID], [Date], [Procedure_ID], [Pet_ID], [Vet_ID], [Devices_ID], [Cost], [Services_ID], [Active], [History_ID]) VALUES (5, NULL, 2, 2, 2, 2, CAST(200 AS Decimal(18, 0)), 1, 1, 1)
SET IDENTITY_INSERT [dbo].[Schedule] OFF
SET IDENTITY_INSERT [dbo].[Services] ON 

INSERT [dbo].[Services] ([ID], [Type], [Cost], [Active]) VALUES (1, N'Bath', CAST(50 AS Decimal(18, 0)), 1)
INSERT [dbo].[Services] ([ID], [Type], [Cost], [Active]) VALUES (2, N'Hair Cut', CAST(25 AS Decimal(18, 0)), 1)
INSERT [dbo].[Services] ([ID], [Type], [Cost], [Active]) VALUES (3, N'Brush Teeth', CAST(15 AS Decimal(18, 0)), 1)
SET IDENTITY_INSERT [dbo].[Services] OFF
SET IDENTITY_INSERT [dbo].[User] ON 

INSERT [dbo].[User] ([ID], [Username], [Password], [Active]) VALUES (1, N'frank', N'happy', 1)
INSERT [dbo].[User] ([ID], [Username], [Password], [Active]) VALUES (2, N'dah', N'Teamprj', 1)
INSERT [dbo].[User] ([ID], [Username], [Password], [Active]) VALUES (3, N'nTacey', N'passy', 1)
INSERT [dbo].[User] ([ID], [Username], [Password], [Active]) VALUES (4, N'nDuncan', N'root', 1)
INSERT [dbo].[User] ([ID], [Username], [Password], [Active]) VALUES (5, N'happy', N'happy', 1)
INSERT [dbo].[User] ([ID], [Username], [Password], [Active]) VALUES (6, N'frank', N'tank', 1)
INSERT [dbo].[User] ([ID], [Username], [Password], [Active]) VALUES (7, N'sally', N'sue', 1)
INSERT [dbo].[User] ([ID], [Username], [Password], [Active]) VALUES (8, N'jon', N'jon', 1)
SET IDENTITY_INSERT [dbo].[User] OFF
SET IDENTITY_INSERT [dbo].[Vets] ON 

INSERT [dbo].[Vets] ([ID], [Role_ID], [Active]) VALUES (1, 1, 1)
INSERT [dbo].[Vets] ([ID], [Role_ID], [Active]) VALUES (2, 3, 1)
INSERT [dbo].[Vets] ([ID], [Role_ID], [Active]) VALUES (3, 2, 1)
INSERT [dbo].[Vets] ([ID], [Role_ID], [Active]) VALUES (4, 1, 1)
SET IDENTITY_INSERT [dbo].[Vets] OFF
ALTER TABLE [dbo].[AssociatedProcedure]  WITH CHECK ADD  CONSTRAINT [FK_AssociatedProcedure_Devices] FOREIGN KEY([MicSupply_ID])
REFERENCES [dbo].[MicSupply] ([ID])
GO
ALTER TABLE [dbo].[AssociatedProcedure] CHECK CONSTRAINT [FK_AssociatedProcedure_Devices]
GO
ALTER TABLE [dbo].[AssociatedProcedure]  WITH CHECK ADD  CONSTRAINT [FK_AssociatedProcedure_Drugs] FOREIGN KEY([Drugs_ID])
REFERENCES [dbo].[Drugs] ([ID])
GO
ALTER TABLE [dbo].[AssociatedProcedure] CHECK CONSTRAINT [FK_AssociatedProcedure_Drugs]
GO
ALTER TABLE [dbo].[AssociatedProcedure]  WITH CHECK ADD  CONSTRAINT [FK_AssociatedProcedure_MicSupply] FOREIGN KEY([MicSupply_ID])
REFERENCES [dbo].[MicSupply] ([ID])
GO
ALTER TABLE [dbo].[AssociatedProcedure] CHECK CONSTRAINT [FK_AssociatedProcedure_MicSupply]
GO
ALTER TABLE [dbo].[HIstory]  WITH CHECK ADD  CONSTRAINT [FK_HIstory_Pet] FOREIGN KEY([Pet_ID])
REFERENCES [dbo].[Pet] ([ID])
GO
ALTER TABLE [dbo].[HIstory] CHECK CONSTRAINT [FK_HIstory_Pet]
GO
ALTER TABLE [dbo].[HIstory]  WITH CHECK ADD  CONSTRAINT [FK_HIstory_Schedule] FOREIGN KEY([Schedule_ID])
REFERENCES [dbo].[Schedule] ([ID])
GO
ALTER TABLE [dbo].[HIstory] CHECK CONSTRAINT [FK_HIstory_Schedule]
GO
ALTER TABLE [dbo].[Person]  WITH CHECK ADD  CONSTRAINT [FK_Person_User] FOREIGN KEY([User_ID])
REFERENCES [dbo].[User] ([ID])
GO
ALTER TABLE [dbo].[Person] CHECK CONSTRAINT [FK_Person_User]
GO
ALTER TABLE [dbo].[Procedure]  WITH CHECK ADD  CONSTRAINT [FK_Procedure_Devices] FOREIGN KEY([Devices_ID])
REFERENCES [dbo].[Devices] ([ID])
GO
ALTER TABLE [dbo].[Procedure] CHECK CONSTRAINT [FK_Procedure_Devices]
GO
ALTER TABLE [dbo].[Procedure]  WITH CHECK ADD  CONSTRAINT [FK_Procedure_Drugs] FOREIGN KEY([Drugs_ID])
REFERENCES [dbo].[Drugs] ([ID])
GO
ALTER TABLE [dbo].[Procedure] CHECK CONSTRAINT [FK_Procedure_Drugs]
GO
ALTER TABLE [dbo].[Procedure]  WITH CHECK ADD  CONSTRAINT [FK_Procedure_MicSupply] FOREIGN KEY([MicSupply_ID])
REFERENCES [dbo].[MicSupply] ([ID])
GO
ALTER TABLE [dbo].[Procedure] CHECK CONSTRAINT [FK_Procedure_MicSupply]
GO
ALTER TABLE [dbo].[Schedule]  WITH CHECK ADD  CONSTRAINT [FK_Schedule_Devices] FOREIGN KEY([Devices_ID])
REFERENCES [dbo].[Devices] ([ID])
GO
ALTER TABLE [dbo].[Schedule] CHECK CONSTRAINT [FK_Schedule_Devices]
GO
ALTER TABLE [dbo].[Schedule]  WITH CHECK ADD  CONSTRAINT [FK_Schedule_Pet] FOREIGN KEY([Pet_ID])
REFERENCES [dbo].[Pet] ([ID])
GO
ALTER TABLE [dbo].[Schedule] CHECK CONSTRAINT [FK_Schedule_Pet]
GO
ALTER TABLE [dbo].[Schedule]  WITH CHECK ADD  CONSTRAINT [FK_Schedule_Procedure] FOREIGN KEY([Procedure_ID])
REFERENCES [dbo].[Procedure] ([ID])
GO
ALTER TABLE [dbo].[Schedule] CHECK CONSTRAINT [FK_Schedule_Procedure]
GO
ALTER TABLE [dbo].[Schedule]  WITH CHECK ADD  CONSTRAINT [FK_Schedule_Vets] FOREIGN KEY([Vet_ID])
REFERENCES [dbo].[Vets] ([ID])
GO
ALTER TABLE [dbo].[Schedule] CHECK CONSTRAINT [FK_Schedule_Vets]
GO
ALTER TABLE [dbo].[Vets]  WITH CHECK ADD  CONSTRAINT [FK_Vets_Role] FOREIGN KEY([Role_ID])
REFERENCES [dbo].[Role] ([ID])
GO
ALTER TABLE [dbo].[Vets] CHECK CONSTRAINT [FK_Vets_Role]
GO
USE [master]
GO
ALTER DATABASE [APP] SET  READ_WRITE 
GO

2 个答案:

答案 0 :(得分:0)

类Person中@OneToOne中的mappedBy attribute应指向用户对您已声明为" hPerson"的人员的引用。

public class Person
{
    ...
    @OneToOne(mappedBy = "user")
    private User User;

应该是

    ...
    @OneToOne(mappedBy = "hPerson")
    private User User;

正如声明的那样,您说用户拥有与Person的关系。

答案 1 :(得分:0)

根据JavaDocs for @OneToOne,有两个用例在两个普通(即非嵌入或组件)实体类之间具有一对一的关系:

案例1 :这两个类共享主键。这被建模为:

@Entity
class User {
  @Id
  private Long id;

  @OneToOne
  private Person person;
}

@Entity
class Person {
  @Id
  private Long id;

  @OneToOne
  private User user;
}

根据域和类模型的业务规则,可能会添加其他可选信息,例如@OneToOne(optional = true)@OneToOne @MapsId(有关详细信息,请参阅JavaDocs)。但是,这个基本模型有效。

案例2 :这两个类通过外键具有逻辑一对一的关系。这被建模为:

@Entity
class User {
  @Id
  private Long id;

  @OneToOne
  @JoinColumn(name = "person_id")
  private Person person;
}

@Entity
class Person {
  @Id
  private Long id;

  @OneToOne(mappedBy = "person")
  @JoinColumn(name = "user_id")
  private User user;
}

在这种情况下,owned实体必须通过在owner上包含mappedBy属性来声明@OneToOne实体如何引用其实例。在上面的示例中,假设User是关系的所有者,而Person是拥有的实体(因此mappedBy方需要Person

比较两个示例,可以看出使用外键列对一对一关系建模需要在具有外键的一侧使用@JoinColumn注释。

你拥有的是这两种情况的混合体。在您的实体模型中,User没有指向Person的外键列,但Person的外键列返回User。因此,可以使用上面第一个示例中的User定义和第二个示例中的Person定义来开发最简单的模型:

@Entity
class User {
  @Id
  private Long id;

  @OneToOne
  private Person person;
}

@Entity
class Person {
  @Id
  private Long id;

  @OneToOne
  @JoinColumn(name = "user_id")
  private User user;
}

查看User中的课程Personthis sample app。您可以运行关联的JUnit测试用例或检查CI构建状态以确保关联正常工作。