如果我能将我的模型子集合放入包含所述子集合的值的组合框中,我将非常高兴。我正在开发的应用程序是javafx和spring的混合体。我使用javafxforgui和springdatajpa与我的数据库进行交互,其他地方的数据库正由springbootsrestapi处理。我在gui应用程序中使用的模型是restapi中其他地方使用的模型的副本。
这是模型。
@Entity
@NoArgsConstructor
@Getter
@Setter
@Table(name = "users")
public class User implements Serializable {
private static final long serialVersionUID = 65981149772133526L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USER_ID")
private Long id;
@Column(name = "PROVIDER_USER_ID")
private String providerUserId;
@Column(name = "enabled", columnDefinition = "BIT", length = 1)
private boolean enabled;
@Column(name = "DISPLAY_NAME")
private String displayName;
@Column(name = "created_date", nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
protected Date createdDate;
@Temporal(TemporalType.TIMESTAMP)
protected Date modifiedDate;
private String fullName;
private String phone;
private String company;
private String provider;
private String username;
private String formEmail;
@NotBlank
@Size(max = 50)
@Email
private String email;
@NotBlank
@Size(max = 120)
private String password;
@JsonIgnore
@ManyToMany
@JoinTable(name = "user_role", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") })
private Set<Role> roles;
public User(String username, String email, String pass) {
this.username = username;
this.email = email;
this.password = pass;
}
}
这是榜样。
package com.example.javafx;
import java.io.Serializable;
import java.util.Set;
import javax.persistence.*;
import com.example.javafx.Role;
import com.example.javafx.User;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Getter
@Setter
@NoArgsConstructor
public class Role implements Serializable {
private static final long serialVersionUID = 1L;
public static final String USER = "USER";
public static final String ROLE_USER = "ROLE_USER";
public static final String ROLE_ADMIN = "ROLE_ADMIN";
public static final String ROLE_MODERATOR = "ROLE_MODERATOR";
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ROLE_ID")
private Long roleId;
@Column(name = "USER_ID")
private String name;
// bi-directional many-to-many association to User
@ManyToMany(mappedBy = "roles")
private Set<User> users;
public Role(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Role role = (Role) obj;
if (!role.equals(role.name)) {
return false;
}
return true;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Role [name=").append(name).append("]").append("[id=").append(roleId).append("]");
return builder.toString();
}
}
erole枚举
public enum ERole {
ROLE_USER,
ROLE_MODERATOR,
ROLE_ADMIN
}
回购协议。
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByUsername(String username);
Boolean existsByUsername(String username);
Boolean existsByEmail(String email);
User findByEmail(String email);
}
@Component
public class SimpleUiController {
@Autowired
private UserRepository userRepo;
@FXML
private TableView<User> users_table;
@FXML
private TableColumn user_id_col;
@FXML
private TableColumn<User, Set<Role>> user_roles_col;
public void syncUsersTableWithDB() {
List<User> userFromServer = userRepo.findAll();
users = FXCollections.observableArrayList(userFromServer);
user_id_col.setCellValueFactory(new PropertyValueFactory("id"));
user_email_col.setCellValueFactory(new PropertyValueFactory("email"));
password_hash_col.setCellValueFactory(new PropertyValueFactory("password"));
user_fullname_col.setCellValueFactory(new PropertyValueFactory("fullName"));
user_formEmail_col.setCellValueFactory(new PropertyValueFactory("formEmail"));
user_phone_col.setCellValueFactory(new PropertyValueFactory("phone"));
user_company_col.setCellValueFactory(new PropertyValueFactory("company"));
//user_roles_col.
user_enabled_col.setCellValueFactory(new PropertyValueFactory("enabled"));
user_created_col.setCellValueFactory(new PropertyValueFactory("createdDate"));
user_modified_col.setCellValueFactory(new PropertyValueFactory("modifiedDate"));
users_table.setItems(users);
}
}
您将看到syncuserstablewithdb函数中有一个user\u roles\u col访问权限。我有其他尝试,但我对我的方法没有信心。
我尝试像其他人一样设置propertyvaluefactory,然后尝试将该集合修改为角色字符串,但没有成功。所以我要做的是将集合转换成tableview中组合框中角色字符串的字符串。我希望我已经解释好了,如果我需要进一步解释,请告诉我。
暂无答案!
目前还没有任何答案,快来回答吧!