Java JPA OneTONE加入发行
这是我的SimpleCost实体Java JPA OneTONE加入发行,java,mysql,hibernate,jpa,orm,Java,Mysql,Hibernate,Jpa,Orm,这是我的SimpleCost实体 @Entity @Table(name="Simplecost") public class SimpleCost { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private Long userId; private Long cost; @OneToOne(cascade=CascadeType.ALL,fetch = FetchType.E
@Entity
@Table(name="Simplecost")
public class SimpleCost {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long userId;
private Long cost;
@OneToOne(cascade=CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name = "userId")
private UserBare user;
}
用户裸机:
@Entity
@Table(name="user")
public class UserBare{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long userId;
private String fName;
private String lName;
}
因此,我在填充SimpleCost.user属性时遇到了问题。我需要使用SimpleCost.userId来检索用户表数据并填充SimpleCost.user对象。这是我的数据库结构
数据库表:
使用者
简单成本
CREATE TABLE `simplecost` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`userId` bigint(20) DEFAULT NULL,
`liableCost` decimal(10,2) DEFAULT NULL,
`isActive` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `fk_simplecost_user1_idx` (`userId`),
CONSTRAINT `fk_simplecost_user` FOREIGN KEY (`userId`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;
您不需要在实体(java类)中定义“userId”列。
UserBare
中的一个显然是无用的,SimpleCost
中的一个将被创建/使用,因为您将@JoinColumn(name=“userId”)
添加到了“user”字段中
删除私有长用户id代码>来自两个实体声明
除此之外,您还需要定义什么是“在填充SimpleCost.user属性时遇到问题”。的可能重复项
CREATE TABLE `simplecost` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`userId` bigint(20) DEFAULT NULL,
`liableCost` decimal(10,2) DEFAULT NULL,
`isActive` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id_UNIQUE` (`id`),
KEY `fk_simplecost_user1_idx` (`userId`),
CONSTRAINT `fk_simplecost_user` FOREIGN KEY (`userId`) REFERENCES `user` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;