0

这是我的 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.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 `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`fName` varchar(100) DEFAULT NULL,
`lName` varchar(100) DEFAULT NULL,
`mName` varchar(100) DEFAULT NULL,
`title` varchar(100) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `id_UNIQUE` (`id`)
 ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
 /*!40101 SET character_set_client = @saved_cs_client */;

简单成本

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 */;
4

1 回答 1

1

您不需要在实体(java 类)中定义的 'userId' 列。一个 inUserBare显然是无用的,并且一个 inSimpleCost将被创建/使用,因为@JoinColumn(name = "userId")您添加到“用户”字段'

private Long userId;从两个实体声明中删除。

除此之外,您需要定义什么是“填充 SimpleCost.user 属性时遇到问题”。

于 2013-10-07T02:24:17.483 回答