我有两个表:KT,KTTypeKT表有一字段是:KTTypeNo(也就是表KTType的主键)利用myecilpse映射的时候,发现字段变成了:private KTType ktType;//本来应该是KTTypeNo的相应的映射文件中对应的内容如下:
<many-to-one name="ktType" class="cn.nit.hibernateORM.KTType" fetch="select">
<column name="KTTypeNo" not-null="true" />
</many-to-one>
我当时看到这个,觉得没什么,可是到后来发现要插入数据,因为我客户端有选择KTType的KTTypeName(是它的一个字段),然后再插入到数据库中的表KT中,可是发现并没有提供set,get方法,根本插入不了?这下怎么办?我应该怎么将用户选择的KTType中的KTTypeNo插入到KT中?
<many-to-one name="ktType" class="cn.nit.hibernateORM.KTType" fetch="select">
<column name="KTTypeNo" not-null="true" />
</many-to-one>
我当时看到这个,觉得没什么,可是到后来发现要插入数据,因为我客户端有选择KTType的KTTypeName(是它的一个字段),然后再插入到数据库中的表KT中,可是发现并没有提供set,get方法,根本插入不了?这下怎么办?我应该怎么将用户选择的KTType中的KTTypeNo插入到KT中?
这个你生成一个对应的方法不就行了么 这个只要对应关系没错是没的问题的
kt.setKtType(ktType);
ktType.setKt(kt);
//最后保存这两个实例(实例只用保存一个(kt)就够了,因为ktType已经是持久态了,当ktType发生改变时,会hibernate自动把它更新进数据库的)
把整张表放到了KT里面
你要获得No通过ktType.KTTypeNo出来
KTType ktType = new KTType();
kt.setKtType(ktType);
ktType.setKTTypeNo(new Integer("1"));//假设是值"1"
这样是不是会将"1"插入到表KT中?
public static int charnum = 0;
public static String newsentence = ""; public static void deleteso(String sentence) {
String[] words = sentence.split(" ");
wordnum = words.length;
for (int i = 0; i < words.length; i++) {
if (words[i].length() != 1) {
StringBuilder newword = new StringBuilder();
for (int j = 0; j < words[i].length(); j++) {
if (returnsm(words[i].charAt(j)) != 'a'
&& returnsm(words[i].charAt(j)) != 'e'
&& returnsm(words[i].charAt(j)) != 'i'
&& returnsm(words[i].charAt(j)) != 'o'
&& returnsm(words[i].charAt(j)) != 'u') {
if (j > 0 && newword.length() > 0) {
if (words[i].charAt(j) != newword.charAt(newword
.length() - 1)
|| words[i].charAt(j) < 65
|| words[i].charAt(j) > 122) {
newword.append(words[i].charAt(j));
} else {
charnum++;
}
} else {
newword.append(words[i].charAt(j));
}
} else {
charnum++;
}
}
words[i] = newword.toString();
}
newsentence = newsentence + words[i] + " ";
}
} public static char returnsm(char c) {
char cf = c; if (c >= 'A' && c <= 'Z') {
cf = (char) (c - 32);
}
return cf;
} public static void main(String[] args) {
System.out.println("Please enter a Sentence:");
String sentence = "";
Scanner read = new Scanner(System.in);
sentence = read.nextLine();
deleteso(sentence);
System.out.println("Abbreviated String: " + newsentence);
System.out.println("Sentence Stats");
System.out.println("Number of Words: " + wordnum);
double a = wordnum;
double b = charnum;
double c = b / a;
System.out.println("Average number of letters dropped per word: " + c);
System.out.println("Do you want to try another? (y/n)");
read = new Scanner(System.in);
String yesno = "y";
yesno = read.nextLine();
if (yesno.equals("y") || yesno.equals("Y")) {
charnum = 0;
newsentence = "";
main(args);
} else {
read.close();
}
}}
最後一遍了!!
KTType ktType = new KTType();
ktType.setKTTypeNo(new Integer("1"));//假设值
kt.setKtType(ktType);
private Integer ktTypeNo;利用“唯一外键关联”实现,是不是可以这样?